We know that in the software development process, sometimes we need to create a very complex object, which is usually composed of multiple sub-components according to certain steps.
For example, when we assemble a computer ourselves, we need to assemble it from CPU, motherboard, memory, hard disk, graphics card, chassis, monitor, keyboard, mouse and other components. For example, if a school needs to purchase 100 computers, it is impossible for the school to buy the parts and assemble them by itself. It must tell the buyer what configuration it needs, and then the buyer will go to the computer company to buy them. The company will assemble them for you and then hand them over to the buyer. , and finally handed it over to the school.
As can be seen from the above example, each component can be flexibly selected (including the level of various computer configurations), but the creation steps are similar. However, the creation of this type of product cannot be described by the factory pattern introduced earlier. Only the builder pattern can well describe the creation of this type of product.
Definition of Builder Pattern:
Separates the construction of a complex object from its representation so that the same construction process can create Differently represented, such a design pattern is called the builder pattern. It breaks down a complex object into multiple simple objects and then builds it step by step. It separates change from immutability, that is, the components of the product remain unchanged, but each part can be flexibly selected.
Summary in one sentence: Separate the construction of a complex object from its representation, so that the same construction process can create different representations
Type: Create a class pattern
Advantages:
Good encapsulation, separation of construction and presentation
Extensibility Better, each specific builder is independent of each other, which is conducive to system decoupling
The client does not need to know the details of the internal composition of the product, the builder can gradually refine the creation process, Without having any impact on other modules, it is easy to control detailed risks
Disadvantages:
The components of the product must be the same, which limits its use Scope
If the internal changes of the product are complex, if the internal changes of the product occur, the builder will also have to make simultaneous modifications, and the later maintenance cost will be larger
Note:
The builder pattern and the factory pattern have different focuses: the builder pattern focuses on the assembly process of components, while the factory method pattern focuses more on the creation process of components, but the two can be used in combination.
The main roles of the builder pattern are as follows:
Product role (Product): It contains multiple Complex objects that make up components, each component is created by a specific builder
Abstract Builder (Builder): Implements the Builder interface to construct and assemble the various components of the product, defined And clarify the representation it creates, and provide an interface for retrieving products
Concrete Builder: Construct an object using the Builder interface to guide the construction process
Director: It calls the component construction and assembly methods in the builder object to complete the creation of complex objects. The director does not involve specific product information
Structural diagram (referenced from the Internet):
For example, now we need to build a car:
Product category:
/** * 产品类 */ public class Car { //车的建造过程 private String wheel; //车轮 private String skeleton; //车架 private String engine; //发动机 public String getWheel() { return wheel; } public void setWheel(String wheel) { this.wheel = wheel; } public String getSkeleton() { return skeleton; } public void setSkeleton(String skeleton) { this.skeleton = skeleton; } public String getEngine() { return engine; } public void setEngine(String engine) { this.engine = engine; } }
Instructor category:
/** * 指导者 */ //汽车总监 public class CarDirector { public Car constructCar(ICarBuilder builder){ builder.buildwheel(); builder.buildSkeleton(); builder.buildEngine(); return builder.buildCar(); } }
Concrete builder:
/** * 具体建造者:生产具体的东西 */ public class ConcreteBuilder implements ICarBuilder{ Car car; public ConcreteBuilder(){ car = new Car(); } @Override public void buildwheel() { car.setWheel("轮子"); } @Override public void buildSkeleton() { car.setSkeleton("车身结构"); } @Override public void buildEngine() { car.setEngine("发动机"); } @Override public Car buildCar() { return this.car; } }
Abstract builder (interface):
/** * 抽象建造者 */ public interface ICarBuilder { public void buildwheel(); //构建车轮 public void buildSkeleton(); //构建骨架 public void buildEngine(); //构建发动机 Car buildCar(); }
Test category:
public class Test { public static void main(String[] args){ //新建一个总监(替你去买汽车) CarDirector director = new CarDirector(); Car car = director.constructCar(new ConcreteBuilder()); System.out.println(car.getWheel()); System.out.println(car.getEngine()); System.out.println(car.getSkeleton()); } }
After reading the code demonstration and combining it with the first part of the article, you can understand it! !
The only difference between the builder pattern and the factory pattern is the creation of complex objects. In other words, if you create simple objects, you usually use the factory pattern to create them, and if you create complex objects, you can consider using the builder pattern
Main application scenarios:
The same method, different execution order, produces different results
Multiple components or parts can be assembled into one object, but the results produced are different
The product class is very complex, or different calling sequences in the product class produce different effects
Initializing an object is particularly complex, with many parameters, and Many parameters have default values
Builder mode pays more attention to the calling order of methods. The factory pattern focuses on creating objects
The intensity of creating objects is different. The builder pattern creates complex objects, which are composed of various complex components. The objects created by the factory pattern are all the same
The focus is different. The factory mode only needs to create the object, while the builder mode not only needs to create the object, but also knows what parts the object is composed of
The builder mode is different according to the order in the construction process, and the final object component composition is also different
The above is the detailed content of What is the builder pattern in Java design patterns?. For more information, please follow other related articles on the PHP Chinese website!