Abstract: in this paper, an example scenario is used to describe the adapter (transformer) pattern in Gof 23 design pattern, which is implemented with Quarkus framework code, and the UML model of the implementation code is also given.
Keywords: Gof 23 design pattern adapter pattern Quarkus
1 basic knowledge
1.1 standard definition
Adapter pattern standard definition: convert the interface of a class into another interface desired by the customer. The adapter pattern allows classes that cannot work together because of interface incompatibility to work together.
1.2 analysis and description
The adapter pattern is a structural design pattern. The interface of a class is transformed into another interface expected by the client, so that the two classes that cannot work together due to interface mismatch can work together. Adapter mode is also called transformer mode and Wrapper mode.
When a class needs a method of another class, but the two classes are not compatible. In terms of implementation principle, there are two methods to achieve this goal: one is inheritance and the other is object combination. In the first method, a new class is derived from a non consistent class, and a new method is added to match the derived class with the required interface; The second method includes the original class in the new class and creates methods to explain the calls in the new class. These two methods are commonly known as class adapter and object adapter. The adapter can be a class adapter or an object adapter.
The Adapter pattern structure is shown in Figure 1, including the Target role, the source role, and the Adapter role.
Figure 1 adapter mode structure
Target role: This is the expected interface and defines the operation expected by the customer. The Target role can be a concrete class or an abstract class. This role is indispensable.
Adaptee role: This is not only our original product, but also the product that needs to be adapted. This is not only our original product, but also the product that needs to be adapted: the source role can be implemented with interfaces, abstract classes and concrete classes. This role is indispensable.
Adapter role: the Adapter role is the core of this mode. The Adapter adapts the Adaptee interface to the Target interface. The Adapter converts the source interface into the Target interface and provides a transition between the Target role and the Adaptee source role, that is, converts the interface provided by the Adaptee source role into the interface provided by the Target role. The concrete Adapter class must be an Adapter. This role is indispensable.
2. Application scenario examples
For example, it may be difficult to communicate directly between the company's customers and the company's design coding developers. But adding a requirements analyst in the middle makes things easier. Customers tell their ideas to the requirements analysts, who transform user requirements into requirements analysis, and tell the design coders how to design and implement. A requirements analyst is an adapter that matches two unrelated people. This is the case in some software outsourcing industries. The use case analysis is shown in Figure 2.
Figure 2 Adapter pattern use case diagram
Here, the Customer analogy can be used as the Target role, the Designer analogy as the Adapter role, and the Analyst analogy as the Adapter role. The implementation class diagram is shown in Figure 3. The Analyst class inherits the Customer class and associates the Designer class.
Figure 3 adapter pattern class diagram
The sequence diagram of the implementation of the adapter mode is shown in Figure 4. The implementation sequence is described as follows: ① create a Customer object; ② Create an analyst object; ③ Create a Designer object; ④ The Customer object calls the commitresulement method to submit requirements to analyst; ⑤ Analyst obtains and analyzes requirements; ⑥ Assign the Designer object to analyst, who gives the analyzed requirements to the Designer object; ⑦ The Designer object completes the development work according to the analysis and returns it to the analyst object, which gives the completed work to the Customer object.
Figure 4 implementation sequence of adapter mode
The Customer object and Designer object cannot work together because they are incompatible, but an Analyst object is added in the middle. The Analyst object transforms the requirements of the Customer object into the Designer object. Then, the work completed by the Designer object is transformed into the Customer object. The Analyst object has a function of "transformation" (adaptation) in it.
3. Implementation program code of Quarkus
Quarkus program implementation mainly includes three files: Customer class file, Analyst class file and Designer class file. The relationship is shown in Figure 3. The program codes of these three files are listed below. Finally, the test codes are listed and the output results are displayed.
The Customer class file program code is shown in listing 01.
Program code list 01
@ApplicationScoped public class Customer { private String customerName; private String requirement = null; //public Customer(){} public void setRequirement(String require) { requirement = require; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getRequirement() { return requirement; } public String commitRequirement() { System.out.println("--User submission requirements————"); return requirement; } }
The program code of the Analyst class file is shown in listing 02.
Program code list 02
public class Analyst extends Customer { private String analystName ; private Designer designer = null; private String request ; public Analyst(){} public Analyst( String name){ analystName = name ; } public String getName() { return analystName; } public void setName(String name) { this.analystName = name; } public Designer getDesigner() { return designer; } public void setDesigner(Designer designer) { this.designer = designer; } public void setRequest(String content){ request = content; } public String getFinishworks(){ String designs = requestToDesign(); designer.setDesigns(designs); return designer.getFinishWork(); } public String requestToDesign(){ System.out.println("--Analysts translate the requirements into requirements analysis and design according to user requirements————"); return getName()+"according to"+request +",Form requirements design content."; } }
The program code of the Designer class file is shown in listing 03.
Program code list 03
@ApplicationScoped public class Designer { private String designerName ; private String works = null; private String Designs = null; public Designer(){ } public Designer(String name){ designerName = name; } public String getFinishWork(){ designToWorks(); return works; } public void setDesign(String Design){Designs = Design; } public void designToWorks(){ System.out.println("--Transform the design into specific work according to the needs analysis————"); works = Designs +getName()+"Design the content according to the requirements and complete the work."; } public String getName() {return designerName;} public void setName(String name) {this.designerName = name; } public String getWorks() {return works;} public String getDesigns() {return Designs; } public void setDesigns(String designs) { Designs = designs; } }
The code listing 04 of the adapter mode test program is as follows:
Program code list 04
public class AdapterClient implements QuarkusApplication { @ConfigProperty(name = "gof23.structuralpattern.adapter.title", defaultValue = "gof23") String title; @Inject Customer customer; @Inject Designer designer ; @Override public int run(String... args) { System.out.println("--" + title + "Demo output——————"); customer.setCustomerName("Customer Xiao Wang"); customer.setRequirement("Customer Xiao Wang's needs"); designer.setName("Developer Xiao Zhang"); Analyst analyst = new Analyst("Analyst Xiao Liu"); //The customer submits the user requirements to the analyst analyst.setRequest(customer.commitRequirement()); //Analysts are transformed and provided to developers analyst.setDesigner(designer); //Get work products that meet customer needs System.out.println(analyst.getFinishworks()); return 0; } public static void main(String... args) { Quarkus.run(AdapterClient.class, args); } }
The output results of adapter mode test class are as follows:
————User submission requirements————
————Analysts translate the requirements into requirements analysis and design according to user requirements————
————Transform the design into specific work according to the needs analysis————
The analyst Xiao Liu forms the requirements design content according to the requirements of the customer Xiao Wang. Developer Xiao Zhang designs the content according to the requirements and completes the work.
4. Download relevant Quarkus program source code
You can get the code directly from github, and readers can clone the pre prepared sample code from github.
git clone https://github.com/rengang66/quarkus-sample-gof23.git
This is a maven project, and then Maven imports the project. The program is located in the "src\main\java\com\iiit\quarkus\sample\gof23\structuralpattern\adapter" directory.
At the same time, you can also use the sample code prepared in advance from clone on gitee. The command is as follows:
git clone https://gitee.com/rengang66/quarkus-sample-gof23.git
reference
[1] E.Gamma, R.Helm, R.Johnson, and Vlissides. Design Patterns Elements of Reusable Object Oriented Software. Addison-Wesley, 1995
[2] E.Gamma, R.Helm, R.Johnson, and Vlissides. Design pattern: the basis of reusable object-oriented software, Beijing: Mechanical Industry Press, 2000.9
[3] Yan Hong, Java and pattern, Beijing: Electronic Industry Press two thousand and two point one zero
[4] Wang Junfeng, Qi Xiaobin Design patterns and UML Computer application research, 1999.16 (5), 27-29,39
[5] Chen Qin, Zhu Zhengqiang Application of UML in design pattern description Computer engineering and design, 2003.24 (4), 81-84
[6] Guo Qinghua, Liu sickle and axe Application of design pattern in network element management system Fujian computer, 2005 (12),31-31,22.
[7] Mavida ACE and GoF design patterns -- Application of Adapter pattern in ACE memory management class Programmer, 2003 (10), 92-93.
[8] Li Zhonghui Z - based formal description of adapter pattern Fujian computer, 2008.24 (7), 80-80100
[9] Chen Dingshan Research and application of design pattern Computer knowledge and technology, 2007 (23), 1346-1348.
[10] Zhao Yongming, Guo min Application of design pattern in radar terminal software system Fire control radar technology, 2008 (2), 93-96.
[11] Zhang Xiaohuai, Chen Yechu, Kou Wei Application of design pattern in equipment fault infrared intelligent diagnosis software Electromechanical equipment, 2007.24 (11), 29-33,13
[12] Lujiang Xiayu people Adapter mode and its application Microcomputer application, 2007.23 (7), 59-61
[12] Hong Zhong Mode application in configuration software design Microcomputer information, 2007 (28), 279-281.
[13] Wang Li, Tang Lingyu The framework model of genetic algorithm class library based on design pattern Science and technology innovation guide, 2008 (3), 7-8.
[14] Xiao Zhifeng, Gong Jianya, Wang Yandong, Zhai Xiaofang Application of object - oriented software design pattern in distribution GIS Surveying and mapping information and engineering, 2005.30 (3), 3-5
[15] Ye Jianping Application of design pattern in amusement facility inspection management information system Microcomputer information, 2005.21 (12X), 213-214,77
[16] Quarkus official website https://quarkus.io/