【Java设计模式】之外观模式

来源:互联网 发布:王使屈平为令众莫不知 编辑:程序博客网 时间:2024/05/21 22:52

1.定义:外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

2.类图


3.代码

子系统类SubSystemOne,其他子系统类类似,此处略。

public class SubSystemOne {public void methodOne(){System.out.println("子系统方法一");}}

外观类

public class Facade {private SubSystemOne one;private SubSystemTwo two;private SubSystemThree three;private SubSystemFour four;public Facade(){one = new SubSystemOne();two = new SubSystemTwo();three = new SubSystemThree();four = new SubSystemFour();}public void methodA(){System.out.println("方法组A");one.methodOne();two.methodTwo();four.methodFour();}public void methodB(){System.out.println("方法组B");two.methodTwo();three.methodThree();}}

客户端

public class ClientTest {public static void main(String[] args) {Facade facade = new Facade();facade.methodA();facade.methodB();}}


原创粉丝点击