Appearance pattern of java design pattern (facade pattern)

Keywords: Java Apache log4j

For appearance mode, it is very frequent in project development and practical application, but it is very easy to understand. Here is a brief introduction.

I. concept introduction

Facade, which hides the complexity of the system and provides the client with an interface to access the system. This type of design pattern belongs to structural pattern. It provides a unified access interface for a group of interfaces in the subsystem, which makes the subsystem easier to access or use.  

II. Roles and usage scenarios

In short, the pattern is to encapsulate some complex processes into an interface for external users to use more easily. In this pattern, three roles are designed.

1). 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.

2). Subsystem role: the function of the subsystem is realized. 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.

3). Customer role: complete the functions to be realized by calling face.

Usage scenario:

1 - modules providing external access for complex modules or subsystems;

2-subsystems are independent of each other;

3-in a tomographic structure, you can use appearance patterns to define the entry of each layer of the system.

Three. Example

Let's implement this pattern with a simple example.

Each Computer has CPU, Memory and Disk. When the Computer is turned on and off, the corresponding components will also be turned on and off. Therefore, when the appearance mode is used, the decoupling between users and components will be enabled. Such as:

Creation of package:

code implementation

First, the subsystem class:

Code List-1

 1 package com.huawei.facadeDesign.children;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 /**
 6  * cpu Subsystem class
 7  * @author Administrator
 8  *
 9  */
10 public class CPU 
11 {
12     public static final Logger LOGGER = Logger.getLogger(CPU.class);
13     public void start()
14     {
15         LOGGER.info("cpu is start...");
16     }
17     
18     public void shutDown()
19     {
20         LOGGER.info("CPU is shutDown...");
21     }
22 }

Code list-2

 1 package com.huawei.facadeDesign.children;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 /**
 6  * Disk Subsystem class
 7  * @author Administrator
 8  *
 9  */
10 public class Disk
11 {
12     public static final Logger LOGGER = Logger.getLogger(Disk.class);
13     public void start()
14     {
15         LOGGER.info("Disk is start...");
16     }
17     
18     public void shutDown()
19     {
20         LOGGER.info("Disk is shutDown...");
21     }
22 }

[code list-3]

 1 package com.huawei.facadeDesign.children;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 /**
 6  * Memory Subsystem class
 7  * @author Administrator
 8  *
 9  */
10 public class Memory
11 {
12     public static final Logger LOGGER = Logger.getLogger(Memory.class);
13     public void start()
14     {
15         LOGGER.info("Memory is start...");
16     }
17     
18     public void shutDown()
19     {
20         LOGGER.info("Memory is shutDown...");
21     }
22 }

Then, Facade

[code list-4]

 1 package com.huawei.facadeDesign.facade;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 import com.huawei.facadeDesign.children.CPU;
 6 import com.huawei.facadeDesign.children.Disk;
 7 import com.huawei.facadeDesign.children.Memory;
 8 
 9 
10 /**
11  * Facade (core)
12  * @author Administrator
13  *
14  */
15 public class Computer
16 {
17     public static final Logger LOGGER = Logger.getLogger(Computer.class);
18     
19     private CPU cpu;
20     private Memory memory;
21     private Disk disk;
22     public Computer()
23     {
24         cpu = new CPU();
25         memory = new Memory();
26         disk = new Disk();
27     }
28     public void start()
29     {
30         LOGGER.info("Computer start begin");
31         cpu.start();
32         disk.start();
33         memory.start();
34         LOGGER.info("Computer start end");
35     }
36     
37     public void shutDown()
38     {
39         LOGGER.info("Computer shutDown begin");
40         cpu.shutDown();
41         disk.shutDown();
42         memory.shutDown();
43         LOGGER.info("Computer shutDown end...");
44     }
45 }

Finally, the customer role.

[code list-5]

 1 package com.huawei.facadeDesign;
 2 
 3 import org.apache.log4j.Logger;
 4 
 5 import com.huawei.facadeDesign.facade.Computer;
 6 
 7 /**
 8  * Client class
 9  * @author Administrator
10  *
11  */
12 public class Cilent {
13     public static final Logger LOGGER = Logger.getLogger(Cilent.class);
14     public static void main(String[] args) 
15     {
16         Computer computer = new Computer();
17         computer.start();
18         LOGGER.info("=================");
19         computer.shutDown();
20     }
21 
22 }

[code list-6] operation result

From the above example, with the Facade class, that is, the Computer class, the user does not need to call the Disk,Memory and CPU classes in the subsystem, and does not need to know the implementation details or even the internal structure of the system. The client only needs to interact with the Facade.

Four, 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 level of access

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.

V. extension and application (Supplement)

  ...

Posted by sharapov on Sun, 03 Nov 2019 02:30:08 -0800