“This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
This is the fifth article on design patterns, in which all the design patterns I will encounter are summarized one by one. I will write down my thoughts on each design pattern and how we can flexibly apply these design patterns in practical work. Welcome your attention. In this article we will look at the Builder pattern for building complex objects.
A brief introduction to the Builder pattern
The responsibilities of the Builder pattern are similar to those of the factory pattern in that they encapsulate the creation process of creating objects. However, the focus of the factory pattern is to provide us with the creation of a series of objects with the same characteristics, while the builder pattern focuses on the creation of complex objects. The Builder pattern allows us to flexibly arrange the construction order of properties within the object.
Class diagram for the Builder pattern
Characters in Builder mode:
- The Director role: The Director’s responsibility is to direct the builder to complete the construction of the product objects in a certain order.
- Abstract Builder: An abstract interface that specifies the parts that create a Product object.
- ConcreteBuilder: Implements the Builder interface, constructs and assembles the parts.
- Product Role (Product)
The concrete realization idea of builder mode
- Create complex objects.
- Create an abstract builder role.
- Create a concrete builder role.
- Create the commander.
The concrete implementation scheme of builder pattern
-
Creating complex objects
public class Product { private String partA; private String partB; private String partC; public void show(){ system.out.println(this) } } Copy the code
-
Create an abstract builder role
public interface Builder { public void setPartA(); public void setPartB(); public void setPartC(); public Product build(); } Copy the code
-
Create a concrete builder role
public class ConcreteBuilder implements Builder { private Product product; public ConcreteBuilder() { product = new Product(); } @Override public void setPartA() { product.setPartA("Part A"); } @Override public void setPartB() { product.setPartB("Part B"); } @Override public void setPartC() { product.setPartC("Part C"); } @Override public Product build() { return product; }}Copy the code
-
Create commander
public class Director { private Builder builder; Public Director(Builder Builder) {this. Builder = Builder; } public Product construct() { builder.setPartA(); builder.setPartB(); builder.setPartC(); return builder.build(); }}Copy the code
Advantages and disadvantages of the Builder model
advantages
- Good encapsulation, build and presentation separation, the client does not need to know the object build process.
- Low coupling, independent of each other.
- The client does not have to know the details of the internal composition of the product, and the builder can gradually refine the creation process without any impact on other modules, facilitating the control of detail risk.
disadvantages
- If the product changes internally, the builder also needs to modify it at the same time, and maintenance costs are high.
Scenarios for the Builder pattern
- When a class constructor has more than four arguments, and some of those arguments are optional, the constructor pattern should be considered.
- Create a complex object with multiple properties to build.
- The Builder pattern compares the order in which relational objects are created internally.
- Building objects in different order produces different results.
- Objects created by the builder are typically made up of multiple parts.
Summary of builder patterns
To sum up, the builder pattern is build, complex object, the object is composed of multiple components, and to control the order of the need to build these components, compared to your house is the object we want to build, so we must first make foundation to build a house, and wall of build by laying bricks or stones, if you don’t is built not a house, according to certain rules and the commander is the contractor, Or the designer of the house, who would have the workers build the house according to certain rules.