Builder: Separate the construction of a complex object from its representation, so that the same construction process can create different representations.
Use scenarios:
General Class Diagram:
For example: Many devices in our lives exist in assembly form, such as desktop computers. Some manufacturers will launch some assembled computer hosts with default configurations (the template method mode can be used here). Customers can purchase products with default configurations, or they can ask manufacturers to reassemble a host with different configurations and assembly methods. At this point, we can use the builder model to meet the requirements of special customers.
Note that in this example, the manufacturer is reassembling a host, that is, the focus is on each component of the host, which is in line with the usage scenario given by the Builder mode above.
The simple code implementation is as follows:
//Abstract product class, using the template method mode, different products have different "component part" abstract class AbstractProduct{ protected abstract void part01(); protected abstract void part02(); protected abstract void part03(); //Template method gives the default assembly method, generating the default product public final AbstractProduct defaultProduct() { part01(); part02(); part03(); return this;//Return the current object, that is, the product with the default assembly method} } //Specific products A and B, different products implement different "component part" class ConcreteProductA extends AbstractProduct{ protected void part01() { System.out.println("Product A: part01() ..."); } protected void part02() { System.out.println("Product A: part02() ..."); } protected void part03() { System.out.println("Product A: part03() ..."); } } class ConcreteProductB extends AbstractProduct{ protected void part01() { System.out.println("Product B : part01() ..."); } protected void part02() { System.out.println("Product B : part02() ..."); } protected void part03() { System.out.println("Product B : part03() ..."); } } //Abstract builder, formulates the combination method that each product should implement buildPart() and standard for producing buildProduct() abstract class AbstractBuilder{ public abstract void buildPart(); public abstract AbstractProduct buildProduct(); } /* * If the specific builder is not satisfied with the default product (i.e. when the defaultProduct() method in the abstract product is called), * You can not call it to obtain the product, but use the specific builder to change the production and assembly method of the product to obtain different products*/ class ConcreteBuilderA extends AbstractBuilder{ private AbstractProduct productA = new ConcreteProductA(); public void buildPart() { this.productA.part03(); this.productA.part02(); this.productA.part01(); } public AbstractProduct buildProduct() { return this.productA; } } class ConcreteBuilderB extends AbstractBuilder{ private AbstractProduct productB = new ConcreteProductB(); public void buildPart() { this.productB.part02(); this.productB.part01(); //One component in product B is omitted, for example, the functions of this part are not needed by customers // this.productB.part03(); } public AbstractProduct buildProduct() { return this.productB; } } //Director class, the builder who pre-holds each product, provides different assembly methods for users who need different products than the default product class Director{ private AbstractBuilder buildA = new ConcreteBuilderA(); private AbstractBuilder buildB = new ConcreteBuilderB(); public AbstractProduct getProductA() { this.builderA.buildPart(); return this.builderA.buildProduct(); } public AbstractProduct getProductB() { this.builderB.buildPart(); return this.builderB.buildProduct(); } } //Test class public class Client { public static void main(String[] args) { System.out.println("Use template method mode to obtain the default product A"); AbstractProduct defaultProductA = new ConcreteProductA().defaultProduct(); System.out.println("/nUse Director class to obtain product A with different assembly methods"); Director director = new Director(); director.getProductA(); System.out.println("/nUse Director class to obtain product B with different assembly methods"); director.getProductB(); } } Test results:
Use template method mode to obtain default product A
Product A: part01() ...
Product A: part02() ...
Product A: part03() ...
Use Director class to obtain products with different assembly methods A
Product A: part03() ...
Product A: part02() ...
Product A: part01() ...
Use Director class to obtain product B with different assembly methods
Product B: part02() ...
Product B: part01() ...
In fact, in this example, the product category uses the template method mode mentioned in the previous article, that is, defaultProduct() provides a method of assembling a default component of the product.
But I have a question here. The so-called default assembly method provided in the AbstractProduct class based on the template method pattern is just to print out a few test sentences, and it does not really return a specific product. However, I don’t know whether the processing method of returning a current object (return this;) in the above example is reasonable?
In addition, after writing these articles about implementing design patterns with Java code, I found that this builder Builder pattern seems to combine abstract factory pattern and template method pattern. The above paragraph has already mentioned my doubts. As for the abstract factory model, I personally think that the Director class in the above code example is very similar to the specific factory class of the abstract factory, but the Director class also has to build the product assembly method before returning a product. Perhaps it is this "build" that makes the builder model focus on the assembly of various parts of the product, while the abstract factory model only focuses on the generation of a final product.
I have read a sentence before and said that it is roughly said: If any computer problem is difficult to solve, it can be handled by adding an intermediate layer. Now that I think about it, it seems that both the Abstract Factory and the Builder mode use this "principle" to achieve the desired effect. For example, there is an abstract factory class in Abstract Factory, and there is a Director class in Builder. In the final analysis, it is to encapsulate and hide certain details and decouple them from the implementation and use.
I think that you must first understand the concerns and applicable scenarios of each model before you can better grasp these.
Maybe these modes are all creative modes and I don’t have any practical experience, which makes me a little confused about these... I’m not afraid, thinking a little bit in the process of implementing them all, and slowly applying them to reality should gradually understand.
The above is all about this article, and I hope it will inspire you to learn.