12 - Design Mode - Appearance Mode

Appearance Mode Concept

The following is excerpted from the rookie tutorial

Intent: To provide a consistent interface for a set of interfaces in a subsystem, the Facade pattern defines a high-level interface that makes this subsystem easier to use.
Main solution: reduce the complexity of accessing the internal subsystems of complex systems and simplify the interface between clients and them. When to use:
1. The client does not need to know the complex connections within the system, and the entire system only needs to provide a "receptionist". 2. Define the entrance of the system.
How to solve: The client is not coupled with the system, and the facade class is coupled with the system. Key code: Add another layer between the client and the complex system, which handles the order of calls, dependencies, etc.
Application examples:
1. Going to the hospital to see a doctor may involve registration, outpatient treatment, price pricing, and medicine collection, which makes the patient or the patient's family feel very complicated. If there is a receptionist, only the receptionist can handle it, which is very convenient.
2, JAVA three-tier development model.
Advantages:
1. Reduce system interdependence.
2. Improve flexibility.
3. Improve the security.
Disadvantages:
It does not conform to the principle of opening and closing. If it is very troublesome to change things, inheritance and rewriting are not suitable.
Usage scenarios: 1. Modules that provide external access to complex modules or subsystems.
2. The subsystems are relatively independent.
3. Prevent the risks brought by low-level personnel.
Note: In a hierarchical structure, you can use the appearance mode to define the entry of each layer in the system.

case

It is known that when you go to the hospital to see a doctor, you may have to register, go to the clinic, price a price, and get medicine, which makes the patient or the patient's family feel very complicated. If there is a receptionist, it is very convenient to only let the receptionist handle it.

submodule

Here, we define four categories of ABCD, which represent the four steps of going to the hospital to see a doctor, namely, registration, outpatient service, pricing, and taking medicine.

public class A {

    public void guahao() {
        System.out.println("挂号");
    }
}
public class B {

    public void mengzheng() {
        System.out.println("门诊");
    }
}
public class C {

    public void huajia() {
        System.out.println("划价");
    }
}
public class D {

    public void quyao() {
        System.out.println("取药");
    }
}

facade

Here, we define the Facade class to represent the reception staff of the hospital, who will complete the operations of registration, outpatient services, pricing, and taking medicines for us.

public class Facade {

    private A a;
    private B b;
    private C c;
    private D d;
    public Facade() {
        a = new A();
        b = new B();
        c = new C();
        d = new D();
    }

    public void treat() {
        a.guahao();
        b.mengzheng();
        c.huajia();
        d.quyao();
    }

}

test class

public class Test {

    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.treat();
    }
}

Adapter Mode, Decorator Mode, and Facade Mode

First, let's take a look at the definitions of adapter mode, decorator mode and appearance mode

  • Adapter pattern: Convert the interface of a class into another interface that the client wants. The adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

How do we implement it here? First of all, we know that the adapter pattern is divided into class adapter and object adapter. The class adapter is mainly through inheritance. The adapter inherits the class that needs to be adapted, and then implements the target interface; the object adapter realizes the target interface by combining the classes that need to be adapted.

  • Decorator Pattern: Dynamically add some additional responsibilities to an object. The decorator pattern is more flexible than subclassing in terms of adding functionality.
  • -

Decorator pattern implementation: First, abstract the class we need to decorate and extract an external interface, because we may have more than one class to decorate. Secondly, we abstract a decoration class and implement the abstract interface of the class that needs to be decorated, which means that our decoration class can decorate all the implementation classes of the decoration interface. The key is here. Next, we create a concrete decoration class to implement our abstract decoration class, hold the classes that need to be decorated through composition in the concrete decoration class, and override the decoration method.

  • Facade Pattern: Provides a consistent interface for a set of interfaces in a subsystem. Facade pattern defines a high-level interface that makes the subsystem easier to use.

Appearance mode implementation: Create an external interface, encapsulate submodules in the interface, and only access the external interface when externally accessed.

The difference between the three:

  • The intent of the facade pattern is to simplify the interface, leaving only one external interface
  • The adapter mode is to replace my existing interface with a different interface so that it can be accessed externally
  • The decorator pattern is based on the existing class and extends some additional responsibilities to it

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325527000&siteId=291194637