03 construction mode Quarkus implementation

Posted by HaLo2FrEeEk on Wed, 12 Jan 2022 07:39:43 +0100

Abstract: This paper describes the construction pattern in Gof 23 design pattern with an example scenario, and implements it with Quarkus framework code. At the same time, it also gives the UML model of the implementation code.
Key words: Gof 23 design mode construction mode Quarkus

1 basic knowledge
1.1 standard definition
Construction pattern standard definition: separate the construction of a complex object from its representation, so that the same construction process can create different representations.
1.2 analysis and description
The construction mode belongs to the creative mode, which is to separate the internal representation of the product from the generation process of the product, so that a construction process can generate product objects with different internal representations. The construction mode allows the internal appearance of the product to change independently, and customers do not need to know the details of the internal composition of the product. The construction mode can enforce a step-by-step construction process. Construction pattern is an ideological method to solve this kind of problem - separating the construction of a complex object from its representation, so that the same construction process can create different representations. The Builder structure is shown in Figure 1.
Builder role includes Abstract builder role, Concrete Builder role, Director role and Product role.
Figure 1 construction mode structure
Abstract builder role: provide an abstract interface to standardize the construction of various components of product objects. In general, interfaces are independent of the application's business logic. In the pattern, it is the Concrete Builder role that directly creates the product object. The specific builder class must implement the two methods required by this interface: one is the construction method; The other is the result return method. Generally speaking, the number of parts contained in the product is consistent with the number of construction methods. In other words, as many components as there are, there are corresponding construction methods.
Concrete Builder role: this role is played by some classes closely related to the application, which create instances of the product under the application call. The tasks to be completed by this role include: implementing the interface declared by the abstract Builder builder, and giving the operation of creating product instances step by step. After the construction process is completed, provide examples of products.
Director role: the class that plays this role calls the concrete builder role to create the product object. The director role does not have the specific knowledge of the product category, and the specific builder role really has the specific knowledge of the product category.
Product role: a product is a complex object under construction. Generally speaking, there will be more than one product class in a system, and these product classes do not necessarily have a common interface, but can be completely unrelated.

2. Application scenario examples
For example, the company needs to do a software project, which is composed of feasibility study, technical exchange, bidding, contract signing, demand research, system design, system coding, system testing, system deployment and implementation, system maintenance and other processes. But for different projects, there are different process components. For example, for non bidding project ProjectA, there are only requirements research, system design, system coding, system testing, system deployment and implementation, system maintenance and other processes. You can use the Builder mode. For example, as shown in Figure 2.
Figure 2 use case diagram of construction pattern
AbstractProjectProcessBuilder can be understood as an abstract builder role, and ConcreteProjectProcessBuilder can be understood as a Concrete Builder role that implements the abstract builder role. ProjectA can be understood as the combination of Director role and Product role. The implementation class diagram is shown in Figure 3.
Figure 3 class diagram of construction mode
The sequence diagram of construction mode implementation is shown in Figure 4. The implementation sequence is described as follows: ① create a builder instance object; ② Create a project1 instance object and assign the builder instance object to project1 object; ③ Obtain project1 object; ④ Construct the project process of project1 object, that is, call its construct method; ⑤ Display the project process in project1.
Figure 4 sequence diagram of construction mode

3. Implementation program code of Quarkus
Quarkus program implementation mainly includes three files: AbstractProjectProcessBuilder abstract class file, ConcreteProjectProcessBuilder class file and ProductA 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.
AbstractProjectProcessBuilder is an abstract builder role, and its class program code is shown in listing 01.
Program code list 01

public abstract class AbstractProjectProcessBuilder {	
	List<String> processList = new ArrayList<String>();
	//Feasibility analysis process
	public void buildFeasibility(){};
	//Technical exchange process
	public void buildTechnicalDiscussion(){};
	//Bidding process
	public void buildBid(){};
	//Demand investigation and analysis process
	public void buildRequirement(){};
	//design process 
	public void buildDesign(){};
	//Coding process
	public void buildProgram(){};
	//Test process
	public void buildTest(){};
	//Deployment and implementation process
	public void buildDeployment(){};
	//Maintenance process
	public void buildMaintenance(){};
	
	public void showProcess(){
		for (int i=0; i<processList.size(); i++){
			System.out.print(processList.get(i) + "  ");
			System.out.println();
		}		
	}
}

ConcreteProjectProcessBuilder is a Concrete Builder role, and its class program code is shown in listing 02.
Program code list 02

@ApplicationScoped
public class ConcreteProjectProcessBuilder extends
		AbstractProjectProcessBuilder {

	// Feasibility analysis process
	public void buildFeasibility() {
		processList.add("Feasibility analysis process");
	}

	// Technical exchange process
	public void buildTechnicalDiscussion() {
		processList.add("Technical exchange process");
	}

	// Bidding process
	public void buildBid() {
		processList.add("Bidding process");
	}

	// Demand investigation and analysis process
	public void buildRequirement() {
		processList.add("Demand investigation and analysis process");
	}

	// design process 
	public void buildDesign() {
		processList.add("design process ");
	}

	// Coding process
	public void buildProgram() {
		processList.add("Coding process");
	}

	// Test process
	public void buildTest() {
		processList.add("Test process");
	}

	// Deployment and implementation process
	public void buildDeployment() {
		processList.add("Deployment and implementation process");
	}

	// Maintenance process
	public void buildMaintenance() {
		processList.add("Maintenance process");
	}
}

ProjectA is the combination of Director role and Product role. The class program code of ProjectA is shown in listing 03.
Program code list 03

@ApplicationScoped
public class ProjectA {
	
	private AbstractProjectProcessBuilder projectBulider;
	public ProjectA(AbstractProjectProcessBuilder  builder){
		projectBulider = builder;
	}
	
	public void setBuilder(AbstractProjectProcessBuilder  builder){
		projectBulider = builder;
	}
	
	public void Construct(){
		projectBulider.buildRequirement();
		projectBulider.buildDesign();
		projectBulider.buildProgram();
		projectBulider.buildTest();
		projectBulider.buildDeployment();
		projectBulider.buildMaintenance();
	}
	
	public void showProcess(){
		projectBulider.showProcess();		
	}
}

The code list 04 of the construction mode test procedure is as follows:
Program code list 04

public class BuilderClient implements QuarkusApplication{	
	
	@ConfigProperty(name = "gof23.creationalpattern.builder.title", defaultValue = "gof23")
	String title;
	
	@Inject
	ConcreteProjectProcessBuilder bulider;
	
	@Inject
	ProjectA project1;
	
	@Override
    public int run(String... args) {		
		System.out.println("---" + title + "Demo output————————");	
		
		project1.Construct();
		project1.showProcess();
        return 0;
    }    
	
	public static void main(String... args) {		
		Quarkus.run(BuilderClient.class, args);		
	}
}

The output results of construction mode test class are as follows:

Requirements investigation and analysis process design process coding process testing process deployment and implementation process maintenance process

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\builder" 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] Kong Jinjin, Gu Chunhua Design patterns support domain - driven design and its application Computer and modernization, 2008 (11),46-49,53.
[7] Non fish Understand design pattern 3 - Builder Internet information world, 2002 (1),87-88.
[8] Guo Qinghua, Liu sickle and axe Application of design pattern in network element management system Fujian computer, 2005 (12),31-31,22.
[9] Ma Hui Asp.net using generator schema and XML to realize custom query Tianjin Science and technology, 2005.32 (4), 39-41
[10] Shen Jianlei, hang Shi Haohong Application of design pattern in optical transmission network management system Computer technology and development, 2007.17 (3), 231-232235
[11] Wang Xiaobo, Cai Hanming, Wang Jun, Cui Huimin Application of design pattern in CAD software development of on - line cutting Electromechanical engineering technology, 2006.35 (2), 44-47
[12] Quarkus official website https://quarkus.io/

Topics: Java Programming Design Pattern