外观模式,为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式的结构图如下所示:
外观模式的代码如下所示:
class SubSystemOne {
public void methodOne() {
System.out.println("method one");
}
}
class SubSystemTwo {
public void methodTwo() {
System.out.println("method two");
}
}
class SubSystemThree {
public void methodThree() {
System.out.println("method three");
}
}
class SubSystemFour {
public void methodFor() {
System.out.println("method four");
}
}
public class Facade {
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade() {
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
public void MethodA() {
System.out.println("method a");
one.methodOne();
two.methodTwo();
four.methodFor();
}
public void MethodB() {
two.methodTwo();
three.methodThree();
}
}
外观模式定义了一个高层接口,在高层接口中封装底层接口的内容,客户端可以直接调用高层接口来使用底层接口。