外观模式
标签:设计模式

外观模式(Facade)

外观模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面称作外观(facade)

1. 外观模式的角色及其职责

1.1 外观(Facade)

为调用方定义简单的调用接口

Facade

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public class Facade {
    private SystemA systemA;
    private SystemB systemB;
    private SystemC systemC;

    public Facade() {
        systemA=new SystemA();
        systemB=new SystemB();
        systemC=new SystemC();
    }

    public void doABC(){
        systemA.doA();
        systemB.doB();
        systemC.doC();
    }

    public void doAC(){
        systemA.doA();
        systemC.doC();
    }
}

1.2 客户端(Clients)

调用者。通过Facade接口调用提供某功能的内部类群

这里我是利用的后面的Main类去调用的

1.3 子系统(Packages)

功能提供者。指提供功能的类群(模块或子系统)

SystemA

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public class SystemA {
    public void doA(){
        System.out.println("A do something");
    }
}

SystemB

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public class SystemB {
    public void doB(){
        System.out.println("B do something");
    }
}

SystemC

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public class SystemC {
    public void doC(){
        System.out.println("C do something");
    }
}

Main

package com.liuyao;

public class Main {

    public static void main(String[] args) {
        Facade facade=new Facade();
        facade.doABC();
        System.out.println("===============");
        Facade facade1=new Facade();
        facade1.doAC();
    }
}

//A do something
//B do something
//C do something
//===============
//A do something
//C do something
  • 2 min read

CONTRIBUTORS


  • 2 min read