This is the 26th day of my participation in the More Text Challenge. For more details, see more text Challenge


👉 Design mode directory

What is the Facade pattern

concept

Facade mode, also known as appearance mode, belongs to structural mode. Personally, I prefer to call it the facade pattern, because this pattern is to hide the internal complexity of the system, to provide a unified interface to the outside, making it easier and simpler for the outside to access the system.

Facade pattern is the representative of Demitt’s law, it is concerned about the combination of objects and the relationship between objects, it is mainly to think of several objects between the complex operation are encapsulated in a high-level class, the external call does not need to care about its internal logic, only need to call the function you want.

We also use the facade pattern in daily development. Those who have used Spring for development must know the hierarchical division of MVC. The Controller layer and Service layer both use the facade pattern, which close the internal complexity and provide a convenient interface for external calls. Let me give a common example, we want to wash clothes, if it is our own washing, need to add detergent to wash, wring, wash the water, and then wring, and then the water, finally need to wring more dry, this step is very much; If we use the washing machine, we just need to pour the clothes into the washing machine, and the washing process is done by the washing machine. Here the washing machine is closed to the washing process, we just need to throw the clothes into the washing machine.

advantages

The facade model satisfies Demeter’s law, and most of its advantages are similar to Demeter’s law.

  1. The coupling between the subsystem and the caller is reduced. The caller only needs to call the facade and does not need to care about how many subsystems are underneath.
  2. Make the invocation more convenient.
  3. It reduces system interdependence and increases system flexibility. As long as the facade remains unchanged, no matter how the logic inside the system changes.

disadvantages

It does not conform to the principle of openness and closure. Every time there is a new change, the facade needs to be modified. If you want to inherit and rewrite, the composition is difficult to complete.

The principle of

“+” means compliance, and “-” means noncompliance or irrelevant

The principle of Open the closed Single responsibility Di milt Replacement on the Richter scale Dependency inversion Interface segregation Synthesis of reuse
+ +

Applicable scenario

  1. There are so many components in a module or subsystem that the invocation process is cumbersome. In this case, you need to use the facade pattern to provide a more convenient interface to call, otherwise the module is very complex to use.
  2. Modules or subsystems are internally independent. In this case, we provide a good black box operation to reduce the difficulty of using the caller.
  3. Prevent the risk of low level personnel. Is to make this module easier to use, so that everyone can use it.

How to implement

To implement the facade pattern (appearance pattern), you need two things:

  1. Facade (facade) classes: responsible for handling the logic that calls the subsystem.
  2. Subsystem class: provides part of the system’s functionality.

On the class diagram

In the code

Here is an example of implementing a washing machine.

Subsystem class: SubSystemA/B/C

/** * Sub-class * Model washing machine component: drum * Created on 2021/6/3. **@author xuxiaobai
 */
public class SubSystemA {
    /** * Roll */
    public void operate(a){
        System.out.println("Roll up..."); }} -- -- -- -- -- -- -- -- -- -- -- -- --/** * Sub-class * Model washing machine component: drainpipe * Created on 2021/6/3. **@author xuxiaobai
 */
public class SubSystemB {
    /** * drain */
    public void operate(a){
        System.out.println("In the drain..."); }} -- -- -- -- -- -- -- -- -- -- -- -- -- -/** * Sub-class * Analog washing machine component: Water pipe * Created on 2021/6/3@author xuxiaobai
 */
public class SubSystemC {
    ** * Add water */
    public void operate(a){
        System.out.println("Add water..."); }}Copy the code

Facade: Facade

/** * Created on 2021/6/3. **@author xuxiaobai
 */
public class Facade {
    private SubSystemA subSystemA=new SubSystemA();
    private SubSystemB subSystemB=new SubSystemB();
    private SubSystemC subSystemC=new SubSystemC();

    /** * Wash clothes */
    public void operate(a){
        System.out.println("Start doing laundry.");
        subSystemC.operate();
        subSystemA.operate();
        subSystemB.operate();
        System.out.println("The first wash is done.");
        subSystemC.operate();
        subSystemA.operate();
        subSystemB.operate();
        System.out.println("The second wash is done.");
        subSystemC.operate();
        subSystemA.operate();
        subSystemB.operate();
        System.out.println("The laundry's done, drip drip."); }}Copy the code

Test class: FacadeTest

/**
 * Created on 2021/6/3.
 *
 * @author xuxiaobai
 */
public class FacadeTest {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.operate();
        /** * Start washing clothes * add water... * Roll up... * In drainage... * After the first wash * add water... * Roll up... * In drainage... * Second wash finished * add water... * Roll up... * In drainage... * The laundry is done, dripping */}}Copy the code

The Facade class internally encapsulates the operation of the washing machine and requires only a call to the Operate method to complete the laundry operation.

conclusion

Facade pattern does not conform to the open closed principle, but this is also our most commonly used design patterns, don’t be so easy to use, also not be too hard, as a Java developer, the design patterns of thought should be born, only to note that each facade classes encapsulate operations are in the same level, like a washing machine, It is possible to repackage the method of switching power supply, but not the method of switching the main gate.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have questions about this article, please comment directly or send me a personal message. If you think my writing is good, you can also support me by clicking “like”

Without permission, shall not be reproduced!