Java design pattern - structural design pattern - appearance pattern
Starting from this column, I will summarize the notes of learning design patterns, class learning and my own summary for your reference.
Reference book: This is how to learn design patterns
Other articles:
1, Structural design mode
Among the 23 design modes of GOF, there are three types of design modes: creative design mode, structural design mode and behavioral design mode.
There are 7 structural design modes: agent mode, adapter mode, bridge mode, decoration mode, appearance mode, sharing mode and combination mode.
Class structured pattern: it is concerned with the combination of classes, and multiple classes are combined into a larger system. Generally, there are only inheritance relations and implementation relations in class structured pattern.
Object structured mode: care about the combination of classes and objects, define the instance object of another class in one class through the association relationship, and then call the corresponding method through this object.
2, Appearance mode
1. Appearance mode definition
Appearance mode is a structural design mode with high frequency of use. It simplifies the interaction between the client and subsystem by introducing an appearance role, provides a unified entry for complex subsystem calls, reduces the coupling between the subsystem and the client, and makes client calls very convenient.
Appearance mode, also known as facade mode, is an object structure mode. Appearance pattern is a concrete implementation of Demeter's law. By introducing a new appearance role, the complexity of the original system can be reduced, and the coupling between customer classes and subsystems can be reduced.
2. Role of appearance mode:
Facade (appearance role): its methods can be called at the client, and the functions and responsibilities of related (one or more) subsystems can be known in the appearance role; Under normal circumstances, it delegates all requests sent from the client to the corresponding subsystem and passes them to the corresponding subsystem object for processing.
SubSystem (SubSystem role): there can be one or more SubSystem roles in the software system. Each SubSystem can be not a separate class, but a collection of classes, which realizes the functions of subsystems; Each SubSystem can be called directly by the client or by the appearance role, which handles the requests passed by the appearance class; The SubSystem does not know the existence of appearance. For the SubSystem, the appearance role is just another client.
3. Characteristics of appearance mode
advantage:
1. Reduce the complexity of the original system by introducing a new appearance role, and reduce the coupling between the customer class and the subsystem (the subsystem referred to is a generalized concept, which can be a class, a functional module, a component of the system or a complete system), making the subsystem easier to use.
2. It realizes the loose coupling relationship between the subsystem and the client, so that the change of the subsystem will not affect the client calling it, just need to adjust the appearance class
3. The modification of the subsystem has no impact on other subsystems, and the internal changes of the subsystem will not affect the appearance object
Disadvantages:
1. It is not good to restrict clients' direct use of subsystem classes. If too many restrictions are made on clients' access to subsystem classes, variability and flexibility will be reduced
2. If the design is improper, adding a new subsystem may need to modify the source code of the appearance class, which violates the opening and closing principle
Applicable environment:
1. Provide a simple access to a series of complex subsystems
2. There is a great dependence between the client program and multiple subsystems
3. In the hierarchical structure, the appearance pattern can be used to define the entry of each layer in the system. There is no direct connection between layers, but the connection is established through appearance classes to reduce the coupling between layers
4. Class diagram of appearance mode
5. Code implementation of appearance mode
5.1 specific cases:
A software company wants to develop a file encryption module that can be applied to multiple software. This module can encrypt the data in the file and store the encrypted data in a new file. The specific process includes three parts: reading the source file, encrypting and saving the encrypted file. Among them, reading and saving the file are realized by stream, and the encryption operation is realized by modular operation. These three operations are relatively independent. In order to realize the independent reuse of code and make the design more in line with the principle of single responsibility, the business code of these three operations is encapsulated in three different classes.
Now we use the appearance mode to design the file encryption module.
The class diagram is as follows:
Specific code implementation:
1. Appearance EncryptFacade
/** * Encrypt the appearance class and act as the appearance class * @author WxrStart * @create 2022-05-31 16:50 */ public class EncryptFacade { //Maintain references to subsystem objects private FileReader reader; private CipherMachine cipher; private FileWriter writer; public EncryptFacade() { reader = new FileReader(); cipher = new CipherMachine(); writer = new FileWriter(); } //Calling business methods of subsystem objects public void fileEncrypt(String fileNameSrc, String fileNameDes) { String plainStr = reader.read(fileNameSrc); String encryptStr = cipher.encrypt(plainStr); writer.write(encryptStr, fileNameDes); } }
2. Subsystem FileReader
/** * File reading subsystem * @author WxrStart * @create 2022-05-31 16:46 */ public class FileReader { public String read(String fileNameSrc){ System.out.print("Read the file and get plaintext:"); StringBuffer sb = new StringBuffer(); try { FileInputStream inFS = new FileInputStream(fileNameSrc); int data; while((data=inFS.read())!=-1) { sb = sb.append((char)data); } inFS.close(); System.out.println(sb.toString()); } catch (FileNotFoundException e) { System.out.println("file does not exist"); }catch (IOException e) { System.out.println("File operation error!"); } return sb.toString(); } }
3. Subsystem FileWriter
/** *File saving subsystem * @author WxrStart * @create 2022-05-31 16:49 */ public class FileWriter { public void write(String encryptStr, String fileNameDes) { System.out.println("Save the ciphertext and write the file."); try { FileOutputStream outFS = new FileOutputStream(fileNameDes); outFS.write(encryptStr.getBytes()); outFS.close(); } catch (FileNotFoundException e) { System.out.println("File does not exist!"); }catch (IOException e) { System.out.println("File operation error!"); } } }
4. Subsystem CipherMachine
/** * Data encryption class, acting as subsystem class * @author WxrStart * @create 2022-05-31 16:52 */ public class CipherMachine { public String encrypt(String plainText) { System.out.print("Data encryption, converting plaintext into ciphertext:"); String es = ""; for (int i = 0; i < plainText.length(); i++) { String c = String.valueOf(plainText.charAt(i)%7); es+=c; } System.out.println(es); return es; } }
5. Client test class
/** * @author WxrStart * @create 2022-05-31 16:52 */ public class Client { public static void main(String[] args) { EncryptFacade ef = new EncryptFacade(); ef.fileEncrypt("E:\\IDEAwokspace\\test\\src\\main\\java\\com\\menmian\\hello.txt", "E:\\IDEAwokspace\\test\\src\\main\\java\\com\\menmian\\hellojiami.txt"); } }