1 Definition
Separate the construction of a complex object from its representation, so that the same construction process can create different representations.
Type: creates a class pattern.
Four elements:
- Product class: generally, it is a more complex object, that is to say, the process of creating an object is more complex, and generally there will be more code. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can be composed of an abstract class and its different implementations, or it can be composed of multiple abstract classes and their implementations.
- Abstract Builder: the purpose of introducing Abstract builder is to realize the concrete process of construction by its subclass. This makes it easier to scale. Generally, there are at least two abstract methods, one for building products and the other for returning products.
- Builder: implements all the unimplemented methods of the abstract class. Specifically, there are generally two tasks: build the product; return the product built by the group.
- Director class: responsible for calling appropriate builders to build products. Director class generally does not depend on product class, and the builder class directly interacts with director class. In general, director classes are used to encapsulate the volatile parts of a program.
2 code implementation
class Product { private String name; private String type; public void showProduct(){ System.out.println("Name:+name); System.out.println("Model:+type); } public void setName(String name) { this.name = name; } public void setType(String type) { this.type = type; } } abstract class Builder { public abstract void setPart(String arg1, String arg2); public abstract Product getProduct(); } class ConcreteBuilder extends Builder { private Product product = new Product(); public Product getProduct() { return product; } public void setPart(String arg1, String arg2) { product.setName(arg1); product.setType(arg2); } } public class Director { private Builder builder = new ConcreteBuilder(); public Product getAProduct(){ builder.setPart("BMW ","X7"); return builder.getProduct(); } public Product getBProduct(){ builder.setPart("Audi ","Q5"); return builder.getProduct(); } } public class Client { public static void main(String[] args){ Director director = new Director(); Product product1 = director.getAProduct(); product1.showProduct(); Product product2 = director.getBProduct(); product2.showProduct(); } }
3 Comparison and summary
Advantages of builder mode:
- First of all, the encapsulation of builder pattern is very good. Using builder mode can effectively encapsulate changes. In the scene using builder mode, the general product class and builder class are relatively stable. Therefore, encapsulating the main business logic in the director class can achieve relatively good stability for the whole.
- Second, the builder model is easy to extend. If there are new requirements, it can be completed by implementing a new builder class, basically without modifying the previously tested code, so there will be no risk to the original function.
Differences between builder mode and factory mode:
- The builder mode is very similar to the factory mode. In general, the builder mode only has one more "director" role than the factory mode. In the class diagram of builder mode, if the director class is regarded as the final calling client, then the rest of the diagram can be regarded as a simple factory mode.
- Compared with factory pattern, builder pattern is generally used to create more complex objects. Because the creation process of objects is more complex, the creation process of objects is independent to form a new class director class. In other words, the factory pattern encapsulates all the creation process of objects in the factory class, and the factory class provides the final product to the client. In the builder pattern, the builder class generally only provides the construction of each component in the product class, and delivers the specific construction process to the guide class. The director class is responsible for organizing each component into a product according to specific rules, and then delivering the product built by the group to the client.
Conclusion:
The builder mode is similar to the factory mode. They are all builder modes, and the applicable scenarios are very similar. Generally speaking, if the construction of the product is complex, please use the factory mode; if the construction of the product is more complex, please use the builder mode