1

Start with a picture, and the rest is all about writing...

What is Appearance Mode

The appearance mode is actually used to hide the complexity of the system, shield the complex logic behind it, and provide users with a simple interface to access the system, which is also a type of structural mode.

For example, for example, our Java three-layer 061df799a0ab37 architecture provides MVC controller , but controller may call a lot of service internally, service calls some mapper externally, but it is only an external interface. It looks simple on the outside, looks good, and in fact, you know it.

To give another example, the computers we use are actually extremely complex internally, but when we operate, we no longer care how memory, cpu, disk, and graphics cards work, and even at the lower level, there are binary, hardware, etc. Just turn it on, do what we want to do, such as Ctrl+C , Ctrl+V , and operate on the beautiful and beautiful interface.

Roles in Appearance Mode

Appearance mode mainly includes several roles:

  • Appearance role: Combine the functions of multiple subsystems to provide a common interface to the outside world
  • The role of the subsystem: to realize part of the function of the system
  • Customer Roles: Access the functionality of individual subsystems through Facade Roles

Advantages and disadvantages

advantage:

  • Reduce system dependencies, here refers to external system dependencies
  • Increase flexibility
  • Improve security

shortcoming:

  • Mixing things into one person brings unknown risks
  • Adding new subsystems may require modifying the facade class or the client's source code, violating the "open-closed principle"

test example

Let's take a computer as an example, first define each part of the computer abstractly as a component, and assign a method of work()

public interface Component {
    public void work();
}

Then define three different components of memory, disk and cpu, respectively implement the above interfaces and work separately:

public class Disk implements Component{
    @Override
    public void work() {
        System.out.println("磁盘工作了...");
    }
}

public class CPU implements Component{
    @Override
    public void work() {
        System.out.println("CPU工作了...");
    }
}


public class Memory implements Component{
    @Override
    public void work() {
        System.out.println("内存工作了...");
    }
}

Then the above components may work together, let's simulate the boot process, and the operating system calls them separately:

public class OperationSystem {
    private Component disk;
    private Component memory;
    private Component CPU;

    public OperationSystem() {
        this.disk = new Disk();
        this.memory = new Memory();
        this.CPU = new CPU();
    }

    public void startingUp(){
        System.out.println("准备开机...");
        disk.work();
        memory.work();
        CPU.work();
    }
}

What the user calls is actually the boot-up method of the operating system, and does not directly call the internal method, that is, all details are blocked:

public class PersonTest {
    public static void main(String[] args) {
        OperationSystem operationSystem = new OperationSystem();
        operationSystem.startingUp();
    }
}

The execution result is as follows:

准备开机...
磁盘工作了...
内存工作了...
CPU工作了...

Finally, a brief summary, the appearance mode can become a facade mode, that is, shielding the internal details, only providing external interfaces to achieve the required functions, the internal functions may be very complex, the above we simulate only simple operations. Did you learn it?

【About the author】 :
Qin Huai, Qin Huai Grocery Store 161df799a0b054 ], personal website: http://aphysia.cn , the road of technology is not at one time, the mountains and rivers are long, even if it is slow, it will never stop.

Offer All Solutions PDF

Open Source Programming Notes


秦怀杂货店
147 声望38 粉丝

山高水长,纵使缓慢,驰而不息。