The "Appearance Mode" of design patterns written for myself

Concept reference: https://blog.csdn.net/carson_ho/article/details/54910625
defines a high-level, unified interface through which external access to multiple interfaces of the subsystem is realized

Scenes

  • Provide a simple interface for a complex subsystem
  • Improve the independence of subsystems
  • There are great dependencies between client programs and multiple subsystems, decoupling between systems, high independence and portability
  • In the hierarchical structure, you can use the appearance model to define the entrance of each layer in the system

advantage

  • Reduced the degree of coupling between client classes and subsystem classes
  • The appearance mode shields the subsystem components from the customer, thereby simplifying the interface, reducing the number of objects handled by the customer and making the use of the subsystem easier
  • Reduce the complexity of the original system and the compilation dependency in the system, and simplify the migration process of the system between different platforms

Disadvantage

  • Without introducing abstract appearance classes, adding new subsystems may require modifying the appearance classes or client source code, which violates the "open and close principle"
  • Cannot restrict customers to use subsystem classes well. If too many restrictions on customer access to subsystem classes are made, the variability and flexibility are reduced

use

1. 定义子系统功能接口
/**
 * 功能接口
 *
 * @author dkangel
 */
public interface ISubsystem {
    
    
    /**
     * 打开
     */
    void turnOn();
    /**
     * 关闭
     */
    void turnOff();
}

2. 实现子系统
/**
 * 灯
 *
 * @author dkangel
 */
public class Light implements ISubsystem {
    
    
    @Override
    public void turnOn() {
    
    
        System.out.println("开灯");
    }

    @Override
    public void turnOff() {
    
    
        System.out.println("关灯");
    }
}
/**
 * 电视
 *
 * @author dkangel
 */
public class Television implements ISubsystem {
    
    
    @Override
    public void turnOn() {
    
    
        System.out.println("打开电视");
    }

    @Override
    public void turnOff() {
    
    
        System.out.println("关闭电视");
    }
}

3. 定义外观类
/**
 * 外观类
 *
 * @author dkangel
 */
public class Facade {
    
    
    private ISubsystem light;
    private ISubsystem tv;

    /**
     * 构造函数,实例化成员变量
     *
     * @param light 灯
     * @param tv    电视
     */
    public Facade(ISubsystem light, ISubsystem tv) {
    
    
        this.light = light;
        this.tv = tv;
    }

    /**
     * 打开操作
     */
    public void on() {
    
    
        light.turnOn();
        tv.turnOn();
    }

    /**
     * 关闭操作
     */
    public void off() {
    
    
        light.turnOff();
        tv.turnOff();
    }
}

4. 使用
/**
 * 外观模式使用类
 *
 * @author dkangel
 */
public class Main {
    
    
    public static void main(String[] args) {
    
    
        ISubsystem light = new Light();
        ISubsystem tv = new Television();
        // 不使用外观模式
        turnOnWithNoFacade(light, tv);
        turnOffWithNoFacade(light, tv);

        // 使用外观模式,直观看到使用了外观模式后,客户端调用更简单,知道的东西更少
        Facade facade = new Facade(light, tv);
        turnOnWithFacade(facade);
        turnOffWithFacade(facade);
    }

    private static void turnOnWithNoFacade(ISubsystem light, ISubsystem tv) {
    
    
        light.turnOn();
        tv.turnOn();
    }

    private static void turnOffWithNoFacade(ISubsystem light, ISubsystem tv) {
    
    
        light.turnOff();
        tv.turnOff();
    }

    private static void turnOnWithFacade(Facade facade) {
    
    
        facade.on();
    }

    private static void turnOffWithFacade(Facade facade) {
    
    
        facade.off();
    }
}

Difference from adapter mode

  • The core of the realization of the appearance mode is mainly to store the references of each subsystem by the appearance class, and realize that a unified appearance class is used to package multiple subsystem classes. However, the client only needs to refer to this appearance class, and then call it by the appearance class. Methods in each subsystem
  • The adapter pattern is to wrap an object to change its interface, and the appearance is to "wrap" a group of objects to simplify its interface. Their intentions are different. The adapter converts the interface into a different interface, while the appearance mode is to provide a unified interface to simplify the interface.

Guess you like

Origin blog.csdn.net/Dkangel/article/details/105786267