design pattern——外观方式
design pattern——外观模式
针对问题:在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化。 那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子系统与客户程序之间的依赖解耦?
为子系统中的一组接口提供一个一致的界面, Facade 模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。——Gang of Four
外观模式结构图:
外观模式实现代码:
/** * 实例A * @author bruce * */ public class A { public void methodA(){ System.out.println("A doing something"); } } /** * 实例B * @author bruce * */ public class B { public void methodB(){ System.out.println("B doing something"); } } /** * 实例C * @author bruce * */ public class C { public void methodC(){ System.out.println("C doing something"); } } /** * 外观实例 * @author bruce * */ public class Facade { private A a; private B b; private C c; public Facade(A a,B b,C c){ this.a=a; this.b=b; this.c=c; } /** * 外观接口 */ public void action(){ a.methodA(); b.methodB(); c.methodC(); //....... } } /** * 测试 * @author bruce * */ public class Client { public static void main(String[] args) { A a=new A(); B b=new B(); C c=new C(); Facade facade=new Facade(a,b,c); facade.action(); /** * output: A doing something B doing something C doing something */ } }