外观模式

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

 

 图示代码:

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("A方法执行。。。");
        one.methodOne();
        two.methodTwo();
    }
    public void methodB(){
        System.out.println("B方法执行。。。");
        three.methodThree();
        four.methodFour();
    }
    
    
}
Facade
public class SubSystemOne {
    public void methodOne(){
        System.out.println("methodOne方法执行。。。");
    }

}
SubSystemOne
public class SubSystemTwo {

    public void methodTwo(){
        System.out.println("methodTwo方法执行。。。");
    }
}
SubSystemTwo
public class SubSystemThree {
    public void methodThree(){
        System.out.println("Three方法执行。。。");
    }
}
SubSystemThree
public class SubSystemFour {
    public void methodFour(){
        System.out.println("Four方法执行。。。");
    }
}
SubSystemFour
public class TestFacade {
    public static void main(String[] args) {
        Facade fa = new Facade();
        fa.methodA();
        fa.methodB();
    }
}

结果:
A方法执行。。。
methodOne方法执行。。。
methodTwo方法执行。。。
B方法执行。。。
Three方法执行。。。
Four方法执行。。。
test

 

posted @ 2020-08-12 14:53  就是你baby  阅读(80)  评论(0编辑  收藏  举报