Java Design Pattern-Facade Mode

The role of appearance mode:

Loosely coupled, the appearance mode loosens the coupling relationship between the client and the subsystem, making the modules inside the subsystem easier to expand and maintain.

Simple and easy to use, the appearance mode makes the subsystem easier to use. The client no longer needs to understand the internal implementation of the subsystem, and does not need to interact with the modules inside many subsystems. It only needs to interact with the facade class.
Better division of access levels-Through the proper use of facades, we can help us better divide the access levels. Some methods are external to the system, and some methods are used internally by the system. Centralize the functions that need to be exposed to the outside, so that it is convenient for the client to use and also hides the internal details.

Role of appearance mode:

SubSystem: Subsystem role. Represents a system's subsystems or modules.

Facade: The appearance role. The client controls the role of the subsystem by manipulating the appearance role. For the client, the appearance role is like a barrier, shielding the client from the concrete implementation of the subsystem.

Case study
Suppose a computer contains CPU (Processor), Memory (Memory) and Disk (hard disk). If you want to start the computer, you must start the CPU, Memory, Disk. The same goes for shutdown.
But in fact, we don't need to operate these components when the computer is turned on / off, because the computer has already handled it for us and hides these things.

These components are like subsystem roles, and a computer is an appearance role.

 
SubSystem role

public class CPU {
     public void startup () {
         System.out.println ("cpu startup!");
     }
     public void shutdown () {
         System.out.println ("cpu shutdown!");
     }
}
 
public class Memory {
     public void startup () {
         System.out.println ("memory startup!");
     }
     public void shutdown () {
         System.out.println ("memory shutdown!");
     }
}
 
public class Disk {
     public void startup () {
         System.out.println ("disk startup!");
     }
     public void shutdown () {
         System.out.println ("disk shutdown!");
     }
}

 
Facade appearance character

public class Computer {
     private CPU cpu;
     private Memory memory;
     private Disk disk;
 
     public Computer () {
         cpu = new CPU ();
         memory = new Memory ();
         disk = new Disk ();
     }
 
     public void startup () {
         System.out.println ("start the computer!");
         cpu.startup ();
         memory.startup ();
         disk.startup ();
         System.out.println ("start computer finished!");
     }
 
     public void shutdown () {
         System.out.println ("begin to close the computer!");
         cpu.shutdown ();
         memory.shutdown ();
         disk.shutdown ();
         System.out.println ("computer closed!");
     }
}

The specific call is as follows:

Computer computer = new Computer ();
computer.startup ();
computer.shutdown ();

Author: