设计模式学习笔记二十五:门面模式
动机
门面(FACADE)模式 也叫外观模式,属于对象结构型模式。它主要用来将子系统的一系列调用封装成高层次的接口。
回忆
参加接触的第一个模式就是这个模式,第一次从大牛嘴里听说这个模式,云里雾里,高深莫测。几年后回首,默默微笑,无语。当觉得自己有点厉害了,今天却发现依然很菜,永远有不会的东西。
UML结构图:

代码实现
1.子系统接口:
public class ServiceOne {
    public void doSmth() {
        System.out.println("ServiceOne do something...");
    }
}
public class ServiceOne {
    public void doSmth() {
        System.out.println("ServiceOne do something...");
    }
}
2.抽象后的接口
public class FacadeService {
    public void doSmth() {
        ServiceOne one = new ServiceOne();
        ServiceTwo two = new ServiceTwo();
        one.doSmth();
        two.doSmth();
    }
}
3.调用,即Client:
public class Run {
    public static void main(String[] args) {
        // 不使用facade模式
        doWithOutFacade();
        System.out.println("--------");
        // 使用facade模式
        doWithFacade();
    }
    private static void doWithFacade() {
        FacadeService facade = new FacadeService();
        facade.doSmth();
    }
    private static void doWithOutFacade() {
        ServiceOne one = new ServiceOne();
        one.doSmth();
        ServiceTwo two = new ServiceTwo();
        two.doSmth();
    }
}
                    
                
    
                
            
        
浙公网安备 33010602011771号