This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Builder mode
Is to separate the construction of a complex object from its representation, so that the same construction process can create different representations. The builder pattern hides the creation process of complex objects. It abstracts the creation process of complex objects and dynamically creates objects with compound properties through subclass inheritance or overloading.Copy the code
advantages
- Good encapsulation, build and presentation separation.
- Good scalability, each specific builders are independent of each other, conducive to the decoupling of the system.
- The client does not have to know the details of the internal components of the product, and the builder can gradually elaborate the creation process without any impact on other modules, making it easier to control the risk of details.
disadvantages
- The components of the product must be the same, which limits its use.
- If the internal changes of the product are complex, if the internal changes of the product occur, the builder will have to synchronize the changes, and the maintenance costs will be large.
Usage scenarios
When a class has more than four constructor arguments, and some of those arguments are optional, consider using the constructor pattern.
structure
- Product role: The final generated object; It is a complex object consisting of several components, each of which is created by a concrete builder.
- Abstract Builder: An interface that contains abstract methods for creating the various sub-parts of a product, and usually includes a method getResult() that returns a complex product.
- Concrete Builder: Implement the Builder interface and complete the Concrete method of creating each part of a complex product.
- Director: It calls the component construction and assembly methods in the builder object to complete the creation of a complex object, and there is no product-specific information involved in the Director.
The traditional demonstration
1. Products
public class Jacket {
private Integer sleeve;
private Integer buttons;
private Integer pocket;
}
Copy the code
2, Builder class
public abstract class Builder {
abstract void buildSleeve(a);
abstract void buildButtons(a);
abstract void buildPocket(a);
abstract Jacket createJacket(a);
}
Copy the code
3. Concrete Builder class
Build a specific product, specifying the values required for each component of the specific productCopy the code
public class LiningJacketBuilder extends Builder{
private Jacket jacket = new Jacket();
@Override
void buildSleeve(int sleeve) {
this.jacket.setSleeve(sleeve);
}
@Override
void buildButtons(int buttons) {
this.jacket.setButtons(buttons);
}
@Override
void buildPocket(int pocket) {
this.jacket.setPocket(pocket);
}
@Override
Jacket createJacket(a) {
return this.jacket; }}Copy the code
4, Director
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public Jacket construct(int sleeve, int buttons, int pocket) {
this.builder.buildSleeve(sleeve);
this.builder.buildButtons(buttons);
this.builder.buildPocket(pocket);
return this.builder.createJacket(); }}Copy the code
5. Client
public class Click {
public static void main(String[] args) {
Director director = new Director(new LiningJacketBuilder());
Jacket jacket = director.construct(2.5.1);
System.out.println(jacket.toString());
// Jacket [sleeve=2, buttons=5, pocket=1]}}Copy the code
conclusion
The traditional builder pattern and the factory pattern have some types that return a specific product.
- Difference 1: Director is similar to assembling each part of a product into a product, while a factory directly produces a product
- Distinction 2: In the factory pattern, the client instantiates the factory class and then calls the factory method to get the desired product object. In builder mode, the client can not directly call the builder method, but by directing the builder class to guide how to generate the object, including the object assembly process and building steps, it focuses on building a complex object step by step, return a complete object.
- Difference 3: The factory pattern ultimately builds an object, while the builder pattern aims to build a complex immutable object
Simplify the presentation
- Create a static inner class Builder in Jacket and copy the parameters from Computer into the Builder class.
- Create a private constructor for Jacket that takes type Builder
- Create a public constructor in the Builder
- Create a setting function in Builder to assign the optional parameters in Jacket and return an instance of Builder type
- Create a build() method in the Builder to build an instance of Jacket and return it
public class Jacket {
private Integer sleeve;
private Integer buttons;
private Integer pocket;
private Jacket(Builder builder) {
this.sleeve = builder.sleeve;
this.buttons = builder.buttons;
this.pocket = builder.pocket;
}
public static class Builder{
private Integer sleeve;
private Integer buttons;
private Integer pocket;
public Builder(a) {}
public Builder setSleeve(int sleeve) {
this.sleeve = sleeve;
return this;
}
public Builder setButtons(int buttons) {
this.buttons = buttons;
return this;
}
public Builder setPocket(int pocket) {
this.pocket = pocket;
return this;
}
public Jacket build(a){
return new Jacket(this); }}@Override
public String toString(a) {
return "Jacket [sleeve=" + sleeve + ", buttons=" + buttons + ", pocket=" + pocket + "]"; }}Copy the code
Build the object step by step using a chain call on the client side.
public class Click {
public static void main(String[] args) {
Jacket jacket = new Jacket.Builder().setButtons(5).setPocket(1).setSleeve(2).build();
System.out.println(jacket.toString());
// Jacket [sleeve=2, buttons=5, pocket=1]}}Copy the code
conclusion
With the traditional builder pattern, code with a lot of parameters can be very difficult to read and prone to errors. When the refactoring is simplified, the above example code is just passed in three parameters. If the parameters are a dozen or more, the advantages of the Builder pattern will be even more obvious. Passing parameters is more flexible and the code will be more readable.
This article is a personal opinion, different opinions can be discussed in the comments section (*^▽^*).Copy the code