Writing in the front

  • Take notes on learning design patterns
  • Improve flexibility in the use of design patterns

Learning to address

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

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

Refer to the article

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

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

13. Appearance patterns

13.1 Definition and characteristics of appearance patterns

The Facade pattern, also known as the Facade pattern, is a pattern that makes multiple complex subsystems more accessible by providing a consistent interface to them. This pattern has a unified interface to the outside, so the external application does not have to care about the details of the internal subsystem, which can greatly reduce the complexity of the application and improve the maintainability of the program.

The Facade pattern, a typical application of the “Demeter Principle,” has the following major advantages.

  1. The coupling between the subsystem and the client is reduced so that changes to the subsystem do not affect the client classes that invoke it.
  2. Subsystem components are shielded from the customer, reducing the number of objects the customer handles, and making the subsystem easier to use.
  3. Reduced compilation dependencies in large software systems and simplified migration of systems from one platform to another because compilation of one subsystem does not affect other subsystems and does not affect appearance objects.

The main disadvantages of the Facade pattern are as follows.

  1. Failure to properly restrict the use of subsystem classes by customers can easily lead to unknown risks.
  2. Adding new subsystems may require modifying the facade classes or client source code, violating the Open Closed Principle.

13.2 Structure and implementation of the facade pattern

13.2.1 Structure of the facade pattern

The Facade pattern contains the following major roles.

  1. Facade role: Provides a common interface to multiple subsystems.
  2. Sub System roles: Implement parts of the System that can be accessed by customers through facade roles.
  3. Client role: Access the functions of the various subsystems through a facade role.

13.2.2 Code implementation

Relationship between the class diagram

AirCondition

package com.zhuang.facade;

/**
 * @Classname AirCondition
 * @Description 空调类
 * @Date 2021/3/24 19:23
 * @Created by dell
 */

public class AirCondition {
    public void on() {
        System.out.println("空调打开...");
    }

    public void off() {
        System.out.println("空调关闭...");
    }
}

Light

package com.zhuang.facade; /** * @className Light * @description * @date 2021/3/24 19:23 * @Created by Dell */ public class Light {public void () On () {System.out.println(" light on...") {System.out.println(" light on..."); ); } public void off() {System.out.println();} public void off(); ); }}

TV

package com.zhuang.facade; /** * @ClassName TV * @Description * @Date 2021/3/24 19:23 * @Created by Dell */ public class TV {public void on() {system.out. println(" TV turns on..."); ); } public void off() {System.out.println() {System.out.println(); ); }}

SmartAppliancesFacade

package com.zhuang.facade; /** * @ClassName SmartAppliancesFacade * @Description * @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(); } private void on() {light.on();} private void on() {light; tv.on(); airCondition.on(); } private void off() {light. Off ();} private void off() {light. tv.off(); airCondition.off(); } public void say(String message) {if (message.contains(" contains ")) {on();} public void say(String message) {on(); } else if (message.contains(" closed ")) {off(); } else {System.out.println(" I don't understand this command!! "); ); }}}

Client

package com.zhuang.facade; /** * @ClassName Client * @Description * @Date 2021/3/24 19:24 * @Created by Dell */ public class Client { public static void main(String[] args) { SmartAppliancesFacade smartAppliancesFacade = new SmartAppliancesFacade(); SmartAppliancesFacade. Say (" open the electrical appliances "); System.out.println("================="); SmartAppliancesFacade. Say (" appliances off "); }}

13.3 Scenario of application of appearance pattern

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

13.4 source code parsing

When Tomcat is used as the Web container, it receives a request from a browser and wraps the request information into a ServletRequest object, as shown in Figure ①. But if you think about ServletRequest as an interface, it also has a child interface HttpServletRequest, and we know that the Request object must be a child implementation class object of an HttpServletRequest object, which class is it? We can output the Request object, and we will find an object of a class named RequestFacade.

The RequestFacade class uses the facade pattern

Why use facade mode here?

Define the RequestFacade classes to implement the ServletRequest, respectively, as well as 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, so that even if the RequestFacade is turned down in the servlet, the methods in the private member variable object are not accessible. Not only using Request, but also to prevent the methods in the unreasonable access.

Write in the last

  • If my article is useful to you, please click 👍, thank you!
  • Let me know in the comments section if you have any questions! 💪