Definition (From Baidu Encyclopedia):
The core idea is to separate a "complex object construction algorithm" from its "components and assembly methods", so that the component algorithms and assembly methods can Respond to changes independently;
Reusing the same construction algorithm can create different representations, and different construction processes can reuse the same component assembly method
UML Class Diagram:
##Specific code:
public class Client {public static void main(String[] args) { Director d = new Director(new ConcreteBuilder()); d.construct(); } }public class Director { Builder builder; Director(Builder builder){this.builder = builder; }void construct(){ builder.buildPart(); } }public class ConcreteBuilder implements Builder {private Product product;public Product getResult() {return product; } @Overridepublic void buildPart() { } }public class Product { }
For example:
A car is composed of many parts, ranging from the engine to the rearview mirror. It is obviously unrealistic to assemble a car and give it to the user. After all, what the user wants is just a He doesn't care how you build a car.For example, if I want an Audi, then when it comes to the above example, I will tell the Director that I want to construct an Audi.
Then Director finds the Builder interface (ConcreteBuilder instance) corresponding to Audi. ConcreteBuilder knows the various parts and steps of building Audi.
For example, first build a big frame, then choose an engine, then choose a suitable tire, and finally press A rearview mirror, these steps are the process of buildPart. In short, it is a very complicated process,
but for users, it is Audi and does not care about these complicated processes.
Also, this example seems to be very similar to the abstract factory, but there is an important difference. The factory is only responsible for producing the various parts of the car and is not responsible for assembling it.
This is an important part to distinguish between the two modes.
Builder: Provides an abstract interface to standardize the construction of each component of the product object. This interface specifies which parts of the complex object are to be created, and does not involve the creation of specific object components.
Corresponding to the above example is the assembly of various parts of the car, such as the frame, engine, etc.ConcreteBuilder: Implements the Builder interface to concretely create each part of a complex object for different business logic. After the building process is complete, provide examples of the product.
Corresponding to the above is to assemble the Audi Builder, and add the wheels of the agricultural machinery step by step...
Director: Call specific builders to create various parts of complex objects. The director does not involve specific product information. It is only responsible for ensuring that all parts of the object are created completely or in a certain order.
The word "Director" means director, and his responsibility is also very clear: scheduling. In the above example, if my idea as a producer is to use Audi, the director will notify ConcreteBuilder to do it.
Product: The complex object to be created.
The one corresponding to the above is Audi.
Advantages: Loose coupling: Breaking down the creation steps of complex products into different methods makes the creation process clearer and enables us to be more precise to control the generation process of complex objects.
Better reusability: Building products and assembling and disassembling make building products reusable.
Disadvantages:
The products created by the builder mode generally have many things in common and their components are similar. If the differences between the products are large, the builder mode is not suitable, so Its scope of use is subject to certain restrictions.
If the internal changes of the product are complex, it may be necessary to define many specific builder classes to implement such changes, causing the system to become very large.
refer to:
The above is the detailed content of Detailed explanation of the creator pattern of design patterns. For more information, please follow other related articles on the PHP Chinese website!