1.定义
为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
2.实现
class SubSystemOne {
public void MethodOne() {
System.out.println("SubSystemOne");
}
}
class SubSystemTwo {
public void MethodTwo() {
System.out.println("SubSystemTwo");
}
}
class SubSystemThree {
public void MethodThree () {
System.out.println("SubSystemThree");
}
}
class SubSystemFour {
public void MethodFour() {
System.out.println("SubSystemFour");
}
}
public class Facade {
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade() {
this.one = new SubSystemOne();
this.two = new SubSystemTwo();
this.three = new SubSystemThree();
this.four = new SubSystemFour();
}
public void MethodA() {
System.out.println("MethodA--");
one.MethodOne();
two.MethodTwo();
three.MethodThree();
}
public void MethodB() {
System.out.println("MethodB--");
two.MethodTwo();
four.MethodFour();
}
}
public class Client {
public static void main(String[] args) {
Facade facade=new Facade();
facade.MethodA();
facade.MethodB();
}
}
3.总结
应用:
1.设计时,构建一个有层次结构的系统,使用Facade模式定义子系统中每层的入口点.
2.减少客户程序与抽象类的实现部分之间存在着很大的依赖性.
3.遗留系统时,开发接口。