面向对象的设计模式 ——外观模式

外观模式:由一个类作为界面,要使用所有的子系统类都需要通过界面类的方法,而他们是没有继承实现关系的。

好处就是不用研究种类繁多的子系统类,只要看明白这个界面的类就可以了。

就好像自己买股票变成买基金一样,你只要研究一个基金就可以了,这个基金会去投资各个股票

经典的三层架构不同层之间都应该要有一个Facade来管理繁杂的子类


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

class SubSystemTwo{
    public void MethodTwo(){
        System.out.println("子系统方法二");
    }
}

class SubSystemThree{
    public void MethodThree(){
        System.out.println("子系统方法三");
    }
}

class SubSystemFour{
    public void MethodFour(){
        System.out.println("子系统方法四");
    }
}
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("\n 方法组A");
        one.MethodOne();
        two.MethodOne();
        four.MethodFour();
    }   
    
    public void MethodB(){
        System.out.println("\n 方法组B");
        two.MethodTwo();
        three.MethodThree();

    }
}
class test{
    public static void main(String args[]){
        Facade facade = new Facade();
        
        facade.MethodA();
        facede.MethodB();  
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39337047/article/details/88608695