1.定义
Facade模式也叫外观模式,是由GOF提出的23中设计模式中的一种。Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面被称作Facade。
2.外观模式的结构
3.外观模式的角色和职责
- Facade--为调用方定义简单的调用接口。
- Clients--调用者。通过Facade接口调用提供某功能的内部类群。
- Packages--功能提供者。指提供功能的类群(模块或子系统)。
4.代码演示
package test.com.facade; /** A子系统*/ public class SystemA {/** A子系统实现功能*/public void dosomething() {System.out.println("实现A子系统功能");} }
package test.com.facade; /** B子系统*/ public class SystemB {/** B子系统实现功能*/public void dosomething() {System.out.println("实现B子系统功能");} }
package test.com.facade; /** C子系统*/ public class SystemC {/** C子系统实现功能*/public void dosomething() {System.out.println("实现C子系统功能");} }
package test.com.facade; /** Facade--只对外提供具体的功能实现*/ public class Facade {private SystemA systemA;private SystemB systemB;private SystemC systemC;public Facade() {this.systemA = new SystemA();this.systemB = new SystemB();this.systemC = new SystemC();}public void doA() {this.systemA.dosomething();}public void doAB() {this.systemA.dosomething();this.systemB.dosomething();}public void doABC() {this.systemA.dosomething();this.systemB.dosomething();this.systemC.dosomething();} }
package test.com.facade; /** 测试代码*/ public class Main {public static void main(String[] args) {Facade facade = new Facade();facade.doA();System.out.println("**********");facade.doAB();System.out.println("**********");facade.doABC();System.out.println("**********");} }