Design pattern learning 11 (Java implementation) – appearance pattern

Time:2021-7-18

Write on the front

  • Take notes on learning design patterns
  • Improve the flexible use of design patterns

Learning address

https://www.bilibili.com/vide…

https://www.bilibili.com/vide…

Reference article

http://c.biancheng.net/view/1…

Project source code
https://gitee.com/zhuang-kang/DesignPattern

13. Appearance mode

13.1 definition and characteristics of appearance pattern

Facade mode is also called facade mode,It is a pattern that provides a consistent interface for multiple complex subsystems to make these subsystems more accessible.This mode has a unified interface to the outside, and the external application does not care about the details of the internal subsystem, which will greatly reduce the complexity of the application and improve the maintainability of the program.

Facade model is a typical application of “Dimiter’s law”,It has the following main advantages.

  1. The coupling degree between subsystem and client is reduced, so that the change of subsystem will not affect the client class that calls it.
  2. It shields subsystem components from customers, reduces the number of objects handled by customers, and makes the subsystem easier to use.
  3. It reduces the compilation dependence of large software system and simplifies the migration process of the system between different platforms, because compiling a subsystem will not affect other subsystems and appearance objects.

The main disadvantages of facade mode are as follows.

  1. It can’t restrict customers to use subsystem classes well, and it is easy to bring unknown risks.
  2. Adding a new subsystem may need to modify the appearance class or the client’s source code, which violates the “open close principle”.

13.2 structure and implementation of appearance pattern

13.2.1 structure of appearance pattern

Facade mode includes the following main roles.

  1. Facade role: provides a common interface for multiple subsystems.
  2. Subsystem role: to realize part of the functions of the system, and customers can access it through the appearance role.
  3. Client role: to access the functions of each subsystem through an appearance role.

13.2.2 code implementation

Relational class diagram

AirCondition

package com.zhuang.facade;

/**
 * @Classname AirCondition
 *@ description air conditioning
 * @Date 2021/3/24 19:23
 * @Created by dell
 */

public class AirCondition {
    public void on() {
        System. Out. Println ("air conditioner on...);
    }

    public void off() {
        System. Out. Println ("air conditioner off...);
    }
}

Light

package com.zhuang.facade;

/**
 * @Classname Light
 *@ description electric lights
 * @Date 2021/3/24 19:23
 * @Created by dell
 */

public class Light {
    public void on() {
        System. Out. Println ("light on...);
    }

    public void off() {
        System. Out. Println;
    }
}

TV

package com.zhuang.facade;

/**
 * @Classname TV
 *@ description TV
 * @Date 2021/3/24 19:23
 * @Created by dell
 */

public class TV {
    public void on() {
        System. Out. Println ("TV on...);
    }

    public void off() {
        System. Out. Println ("TV off...);
    }
}

SmartAppliancesFacade

package com.zhuang.facade;

/**
 * @Classname SmartAppliancesFacade
 *@ description smart speaker appearance class
 * @Date 2021/3/24 19:24
 * @Created by dell
 */

public class SmartAppliancesFacade {
    private Light light;
    private TV tv;
    private AirCondition airCondition;

    public SmartAppliancesFacade() {
        light = new Light();
        tv = new TV();
        airCondition = new AirCondition();
    }

    //The private open method is inaccessible to the outside world
    //One click Open
    private void on() {
        light.on();
        tv.on();
        airCondition.on();
    }

    //The private closing method is inaccessible to the outside world
    //One click Close
    private void off() {
        light.off();
        tv.off();
        airCondition.off();
    }

    //Judgment method
    public void say(String message) {
        If (message. Contains ("open")){
            on();
        }Else if (message. Contains){
            off();
        } else {
            System. Out. Println ("I don't understand what you said!");
        }

    }
}

Client

package com.zhuang.facade;

/**
 * @Classname Client
 *@ description appearance mode test class
 * @Date 2021/3/24 19:24
 * @Created by dell
 */

public class Client {
    public static void main(String[] args) {
        SmartAppliancesFacade smartAppliancesFacade = new SmartAppliancesFacade();
        Smartappliances facade.say ("turn on appliances");
        System.out.println("=================");
        Smartappliances facade.say ("turn off appliances");
    }
}

13.3 application scenarios of appearance mode

  • When building a hierarchical structure system, using appearance pattern to define the entry point of each layer in the subsystem can simplify the dependency relationship between subsystems.
  • When there are many subsystems in a complex system, appearance pattern can design a simple interface for the system to be accessed by the outside world.
  • When there is a great relationship between the client and multiple subsystems, the appearance pattern can be introduced to separate them, so as to improve the independence and portability of subsystems.

13.4 source code analysis

When Tomcat is used as a web container, it receives the request sent by the browser. Tomcat will encapsulate the request information into a ServletRequest object, as shown in Figure 1 below. But consider that ServletRequest is an interface, and it also has a sub interface HttpServletRequest. We know that the request object must be a sub implementation class object of HttpServletRequest object. Which class object is it? By outputting the request object, we will find that it is an object of a class named requestfacade.

The requestfacade class uses the appearance pattern

Why use appearance mode here?

Define the requestfacade class, implement the ServletRequest separately, and define the private member variable request at the same time, and the method implementation calls the request implementation. Then, the requestfacade is converted to ServletRequest and passed to the service method of the servlet. Even if it is converted to requestfacade in the servlet, the method in the private member variable object cannot be accessed. It not only uses the request, but also prevents the method from being unreasonably accessed.

Write at the end

  • If my article is useful to you, please give me some, thank you!
  • If you have any questions, please point them out in the comments area!