Writing in the front

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

Learning to address

www.bilibili.com/video/BV1G4…

www.bilibili.com/video/BV1Np…

Refer to the article

C.biancheng.net/view/1317.h…

Program source codeGitee.com/zhuang-kang…

13. Appearance mode

13.1 Definition and features of appearance mode

The Facade pattern, also known as the Facade pattern, is a pattern that makes multiple complex subsystems more accessible by providing a consistent interface. ** This mode has a unified external interface, external applications do not care about the details of the internal subsystem, which will greatly reduce the complexity of the application, improve the maintainability of the program.

The Facade pattern, a typical application of Demeter’s Rule, has the following major advantages.

  1. The coupling degree between subsystem and client is reduced, so that the change of subsystem does not affect the client class that calls it.
  2. Shielding subsystem components from the customer reduces the number of objects the customer processes and makes the subsystem easier to use.
  3. This reduces compilation dependencies in large software systems and simplifies the migration of systems from platform to platform, because compiling one subsystem does not affect other subsystems, nor does it affect appearance objects.

The main disadvantages of the Facade pattern are as follows.

  1. Failure to properly limit customer use of subsystem classes can easily lead to unknown risks.
  2. Adding new subsystems may require modifications to the facade class or client source code, violating the “open close principle.”

13.2 Structure and implementation of appearance pattern

13.2.1 Appearance mode structure

The Facade pattern contains the following key roles.

  1. Facade Role: Provides a common interface externally for multiple subsystems.
  2. Sub System roles: Implement part of the functions of the System and can be accessed by customers through appearance roles.
  3. Client role: Access the functionality of each subsystem through a facade role.

13.2.2 Code implementation

Relationship between the class diagram

AirCondition

package com.zhuang.facade;

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

public class AirCondition {
    public void on(a) {
        System.out.println("Air conditioning on...");
    }

    public void off(a) {
        System.out.println("Air conditioning off..."); }}Copy the code

Light

package com.zhuang.facade;

/ * * *@Classname Light
 * @DescriptionThe light class *@Date 2021/3/24 19:23
 * @Created by dell
 */

public class Light {
    public void on(a) {
        System.out.println("Light on...");
    }

    public void off(a) {
        System.out.println("Lights off..."); }}Copy the code

TV

package com.zhuang.facade;

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

public class TV {
    public void on(a) {
        System.out.println("Turn on the TV...");
    }

    public void off(a) {
        System.out.println("TV off..."); }}Copy the code

SmartAppliancesFacade

package com.zhuang.facade;

/ * * *@Classname SmartAppliancesFacade
 * @DescriptionIntelligent speaker appearance *@Date2021/3/24 that *@Created by dell
 */

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

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

    // The private open method cannot be accessed by the outside world
    // Open with one key
    private void on(a) {
        light.on();
        tv.on();
        airCondition.on();
    }

    // The private close method is not accessible to the outside world
    // Click close
    private void off(a) {
        light.off();
        tv.off();
        airCondition.off();
    }

    // The method of judgment
    public void say(String message) {
        if (message.contains("Open")) {
            on();
        } else if (message.contains("Closed")) {
            off();
        } else {
            System.out.println("I don't understand your instructions!!"); }}}Copy the code

Client

package com.zhuang.facade;

/ * * *@Classname Client
 * @DescriptionAppearance mode test class *@Date2021/3/24 that *@Created by dell
 */

public class Client {
    public static void main(String[] args) {
        SmartAppliancesFacade smartAppliancesFacade = new SmartAppliancesFacade();
        smartAppliancesFacade.say("Turn on your appliances.");
        System.out.println("= = = = = = = = = = = = = = = = =");
        smartAppliancesFacade.say("Turn off appliances"); }}Copy the code

13.3 Application Scenarios of appearance Mode

  • When building a hierarchical system, using a facade pattern to define entry points for each layer in a subsystem simplifies dependencies between subsystems.
  • When a complex system has many subsystems, facade can design a simple interface for the system to be accessed by the outside world.
  • When there is a large connection between the client and multiple subsystems, the introduction of facade patterns can separate them, thus improving subsystem independence and portability.

13.4 Source Code Parsing

When tomcat is used as a Web container to receive requests from the browser, Tomcat encapsulates the request information into ServletRequest objects, as shown in figure ① below. But if you think about it ServletRequest is an interface, it has a subinterface HttpServletRequest, and we know that the Request object must be a subimplementation of an HttpServletRequest object, which class is it? By outputting the Request object, we can see that it is an object of a class named RequestFacade.

The RequestFacade class uses the facade pattern

Why use facade mode here?

Define the RequestFacade class that implements the ServletRequest, respectively, as well as the private member variable Request, and the implementation of the method calls the implementation of Request. Then, a ServletRequest on the RequestFacade is passed to the Servlet’s Service method, so that the methods in the private member variable object cannot be accessed even if the servlet is turned down to the RequestFacade. Both use Request and prevent methods from being improperly accessed.

Write in the last

  • If my article is useful to you, please give me a click 👍, thank you 😊!
  • If you have any questions, please point them out in the comments section! 💪