package caosc.javaModel.Facade;
public class FacadeSystemOne {
public void systemMethodOne(){
System.out.println("系统A方法!");
}
}
package caosc.javaModel.Facade;
public class FacadeSystemTwo {
public void systemMethodTwo(){
System.out.println("系统B方法!");
}
}
package caosc.javaModel.Facade;
public class FacadeSystemThree {
public void systemMethodThree(){
System.out.println("系统C方法!");
}
}
package caosc.javaModel.Facade;
public class Facade {
FacadeSystemOne one;
FacadeSystemTwo two;
FacadeSystemThree three;
public Facade() {
one = new FacadeSystemOne();
two = new FacadeSystemTwo();
three = new FacadeSystemThree();
}
public void facadeMethodA() {
one.systemMethodOne();
two.systemMethodTwo();
}
public void facadeMethodB() {
three.systemMethodThree();
one.systemMethodOne();
}
}
package caosc.javaModel.Facade;
public class FacadeTest {
public static void main(String[] args) {
Facade fa = new Facade();
fa.facadeMethodA();
fa.facadeMethodB();
}
}
何时使用门面模式:
1、首先在设计初期阶段,有意识将2个不同层分离;如MVC框架,需要在数据访问层与业务逻辑层,业务逻辑层与显示层的层与层之间建立Facade;
2、在开发阶段,子系统不断的重构变得越来越复杂,增加Facade减少它们之间的依赖
3、维护一个大型系统的时候这个系统已经难以维护与扩展,但是又有很多重要的功能必须依赖它,为新系统开发一个Facade类,来提供设计粗糙以及设计高度复杂的遗留代码的比较清晰的接口,让新系统与Facade交互,Facade与遗留代码交换所有复杂的工作;