Abstract: in this paper, an example scenario is used to describe the original model 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 original model pattern Quarkus
1 basic knowledge
1.1 standard definition
Prototype pattern standard definition: specify the type of objects to be created with prototype instances, and create new objects by copying these prototypes.
1.2 analysis and description
Prototype pattern refers to using prototype instances to specify the type of objects to be created, and creating new objects by copying (cloning) these prototypes. Prototype pattern is also a creative pattern. Prototype patterns can be used when a system should be created, composed, and represented independently of its products, and when the classes to be instantiated are specified at run time. The original model pattern is applicable to any hierarchical structure. The structure of the prototype pattern is shown in Figure 1. The disadvantage of the prototype pattern is that each class must be equipped with a cloning method.
Prototype roles include client role, Abstract prototype role and Concrete Prototype role. Prototype is used to declare an interface for copying itself, and ConcretePrototype is used to implement the operation of copying itself; Client is used to make a prototype copy itself to create a new object.
Figure 1 standard structure diagram of original model mode
Client role: the Client class makes a request to create an object.
Abstract Prototype role: This is an abstract role, which is usually implemented by an interface or abstract class. This role gives the interfaces required for all concrete Prototype classes.
Concrete Prototype role: the copied object. This role needs to implement the interface required by the Abstract prototype role.
2. Application scenario examples
For example, the company has its own publicity materials for each product. The composition of each publicity material is to introduce the company first. The second is the introduction of the company's organizational structure. The middle content is the technical introduction of the product, case description, and finally the company's communication and contact information. Therefore, the company introduction, organization and mailing address of the company in the publicity materials for different products are the same. In this way, Prototype can be adopted. Clone an introduction template from the basic company product data, and then explain the technical parameters of each product according to the specific product. The use case is shown in Figure 2.
Figure 2 use case diagram of the original model pattern
Here, AbstractPrototype can be understood as an abstract Prototype role. CompanyBaseIntroduction is a Concrete Prototype role. The implementation class diagram is as follows:
Figure 3 class diagram of the original model pattern
The implementation sequence diagram of the original model is shown in Figure 4. The implementation sequence is described as follows: ① create a company instance object; ② Create a new departA instance object and clone it from the company instance object; ③ Obtain the departA instance object; ④ Call the addsomeintroduction method of departA; ⑤ Displays the contents of departA.
Figure 4 implementation sequence of original model mode
3. Implementation program code of Quarkus
Quarkus program implementation mainly includes AbstractPrototype abstract class file and CompanyBaseIntroduction class file. The relationship is shown in Figure 3. The program codes of these two files are listed below. Finally, the test codes are listed and the output results are displayed.
The AbstractPrototype abstract class program code is shown in listing 01.
Program code list 01
@ApplicationScoped public abstract class AbstractPrototype { public AbstractPrototype cloneMyself() { return this; } public void addSomeIntrouction(String topic, String content) {} public void deleteSomeIntrouction(String key) {} public void showIntrouction() { } }
The program code of the CompanyBaseIntroduction class file is shown in listing 02.
Program code list 02
@ApplicationScoped public class CompanyBaseIntroduction extends AbstractPrototype{ private Map<String, String> introductionMap = new HashMap<String, String>(); public CompanyBaseIntroduction(){ } public CompanyBaseIntroduction(Map<String, String> map){ introductionMap = map; } public AbstractPrototype cloneMyself(){ addSomeIntrouction("Company introduction", "This is the basic introduction of the company"); AbstractPrototype object = new CompanyBaseIntroduction(introductionMap); return object; } public void addSomeIntrouction(String topic, String content) { introductionMap.put(topic, content); } public void deleteSomeIntrouction(String key) { if (introductionMap.containsKey(key)) introductionMap.remove(key); } public void addIntrouctionMap(Map<String, String> map){ introductionMap.putAll(map); } public Map<String, String> getIntroductionMap(){ return introductionMap; } public void showIntrouction() { Iterator<String> it = introductionMap.keySet().iterator(); String key, value; while (it.hasNext()) { key = (String) it.next(); value = (String) introductionMap.get(key); System.out.println("key: " + key + "; value: " + value); } } }
The code listing 03 of the original model test procedure is as follows:
Program code list 03
public class PrototypeClient implements QuarkusApplication { @ConfigProperty(name = "gof23.creationalpattern.prototype.title", defaultValue = "gof23") String title; @Inject CompanyBaseIntroduction company ; @Inject AbstractPrototype departA ; @Inject AbstractPrototype company2 ; @Override public int run(String... args){ System.out.println("---—" + title + "Demo output————————"); departA = company.cloneMyself(); departA.addSomeIntrouction("department A introduce", "This is the Department A Content of introduction"); AbstractPrototype departB = departA.cloneMyself(); departB.deleteSomeIntrouction("department A introduce"); departB.addSomeIntrouction("department B introduce", "This is the Department B Content of introduction"); departB.showIntrouction(); return 0; } public static void main(String... args) { Quarkus.run(PrototypeClient.class, args); } }
The output results of the original model test class are as follows:
key: introduction to department A; value: This is the content introduced by department A
key: Company Introduction; value: This is the basic introduction of the company
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\creationalpattern\prototype" 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
5 extension and description
The original model pattern has been supported at the language level in Java. In the Java language, the top-level parent of all classes is Java Lang.Object class. java.lang.Object class has a clone() method, which is to create and return a copy of this object.
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] Tang mujin, Xu Boqing, sun Guoqiang Dynamic loading mechanism of Java classes and its application in design patterns Journal of Shanghai University of technology, 2004.26 (1), 80-84
[7] Tang Wei, Qu Junhua Design of prototype object pool based on JAVA China Electric Power Education, 2005 (z2), 207-211.
[8] Wan Bo, Zhang Yan, Luo Zhiqiang Application of design pattern in Drawcli program Computer applications and software, 2003.20 (8), 12-13,84
[9] He Zhiguo, Zeng Jun, Chen Tianze, Kuang Gangyao Comprehensive application of design pattern in plotting system Computer application research, 2005.22 (9), 176-179
[10] Hou Xiaohui, Chen Kunyu, Jia Juan, Yang Qingqing The framework model of database publishing system based on design pattern Small microcomputer system, 2004.25 (11), 2044-2048
[11] Quarkus official website https://quarkus.io/