Appearance mode of design mode

1 General

Definition: also known as Facade mode, it is a mode that makes multiple complex subsystems more accessible by providing a consistent interface. This mode has a unified interface to the outside, and the external application does not need to care about the specific details of the internal subsystem, which will greatly reduce the complexity of the application and improve the maintainability of the program. The Facade pattern is a typical application of the "Demeter's law"

2 Structure

The Facade mode contains the following main roles:

  • Facade role: provides a common interface for multiple subsystems.
  • Sub System role: it realizes some functions of the system, and customers can access it through the appearance role.

3 cases

Take Xiaomi smart home as an example to uniformly control household appliances Lamp, TV, air conditioner, etc

Lamps

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

    public void off() {
        System.out.println("Turn off the light....");
    }
}

Television

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

    public void off() {
        System.out.println("Turn off TV....");
    }
}

Air conditioning

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

    public void off() {
        System.out.println("Air conditioning turned off....");
    }
}

Xiaomi smart speaker

public class SmartAppliancesFacade {

    private Light light;
    private TV tv;
    private AirCondition airCondition;

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

    public void say(String message) {
        if(message.contains("open")) {
            on();
        } else if(message.contains("close")) {
            off();
        } else {
            System.out.println("I still don't understand what you said!!!");
        }
    }

    // One button switch
    private void on() {
        light.on();
        tv.on();
        airCondition.on();
    }

    // One button switch off
    private void off() {

        light.off();
        tv.off();
        airCondition.off();
    }
}

Test class

public class Client {
    public static void main(String[] args) {
        // Create appearance objects
        SmartAppliancesFacade facade = new SmartAppliancesFacade();
        // Clients interact directly with appearance objects
        facade.say("Turn on the appliance");
        facade.say("Turn off appliances");
    }
}
/*
Turn on the light
 Turn on the TV
 Turn on the air conditioner
 Turn off the lights
 Turn off TV
 Turn off the air conditioner
*/

Benefits:

  • The coupling between the subsystem and the client is reduced, so that the change of the subsystem will not affect the client class that calls it.
  • It shields subsystem components from customers, reduces the number of objects handled by customers, and makes the subsystem easier to use.

Disadvantages:

  • It does not conform to the opening and closing principle, and it is troublesome to modify

4 usage scenarios

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

5 source code application

When tomcat is used as a web container, the request information will be encapsulated into a ServletRequest pair. ServletRequest is an interface, and it also has a sub interface HttpServletRequest. The request object is a sub implementation class object of an HttpServletRequest object, that is, an object of the RequestFacade class

Instructions for use

The RequestFacade class implements ServletRequest, defines the private member variable Request, and the implementation of the method calls the implementation of the Request. Then, turn the RequestFacade into a ServletRequest and pass it to the service method of the servlet. In this way, even if the RequestFacade is turned down in the servlet, the methods in the private member variable object cannot be accessed. It not only uses Request, but also prevents unreasonable access to the methods.

Tags: Java Design Pattern

Posted by BleedingSky on Thu, 02 Jun 2022 08:47:46 +0530