Java design pattern 8-structural-appearance pattern

Posted by jadeddog on Mon, 04 May 2020 11:47:56 +0200

Appearance mode is very frequent in project development and practical application, but it is very easy to understand.
This pattern is to encapsulate some complex processes into an interface for external users to use more simply.

1. There are three roles involved.

a. facade role: the core of appearance mode. It is called by the customer role, and it is familiar with the functions of the subsystem. In-house, according to the needs of customer roles, several functional combinations are scheduled.
b. subsystem role: realize the function of the subsystem. It's unknown when it comes to customer roles and facades. It can have mutual interaction within the system, and can also be called by the external interface.
c. customer role: complete the function to be realized by calling face.

2. Code demonstration:

    a.3Code of subsystem( CPU,Memory,Disk)
        public class CPU 
        {
            public void start()
            {
                System.out.print("cpu is start...");
            }
            public void shutDown()
            {
                System.out.print("CPU is shutDown...");
            }
        }

        public class Disk 
        {
            public void start()
            {
                System.out.print("Disk is start...");
            }
            public void shutDown()
            {
                System.out.print("Disk is shutDown...");
            }
        }

        public class Memory 
        {
            public void start()
            {
                System.out.print("Memory is start...");
            }
            public void shutDown()
            {
                System.out.print("Memory is shutDown...");
            }
        }

    b.Facade class Computer
        //core
        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 start()
            {
                System.out.print("Computer start begin");
                cpu.start();
                disk.start();
                memory.start();
                System.out.print("Computer start end");
            }

            public void shutDown()
            {
                System.out.print("Computer shutDown begin");
                cpu.shutDown();
                disk.shutDown();
                memory.shutDown();
                System.out.print("Computer shutDown end...");
            }
        }

    c.Client class Cilent
        public class Cilent {
            public static void main(String[] args) 
            {
                Computer computer = new Computer();
                computer.start();
                System.out.print("=================");
                computer.shutDown();
            }
        }
From the above example, with the Facade class, that is, the Computer class, users do not need to call the Disk,Memory and CPU classes in the subsystem,
You don't need to know the implementation details inside the system, or even the composition inside the system. The client only needs to interact with the Facade

3. Advantages

Loose coupling
It makes the client and subsystem decouple, and makes the module function in the subsystem easier to expand and maintain;
Easy to use
The client does not need to know the internal implementation of the subsystem or the internal composition of the subsystem at all. It only needs to interact with the Facade class.
Better division of access levels
Some methods are external to the system, and some methods are used for mutual interaction within the system. The subsystem concentrates the functions exposed to the outside into the facade, so that the client can be used and the details inside the subsystem can be well hidden.