Dr. Yan Hong's book "Java and patterns" describes the builder mode as follows:
The build mode is the object creation mode. The construction model can separate the internal representation of a product (internal representation) from the production process of the product, so that a building process can generate product objects with different internal representations.
Internal appearance of the product
A product usually has different components as parts of the product. These parts may be objects or not objects. They are also called internal representation ). Different products can have different internal representations, that is, different parts. Using the construction mode, the client does not need to know what parts are generated for the product, how the parts corresponding to each product are different from each other, how they are built, and how they constitute the product.
Object Construction
In some cases, an object has some important properties and cannot be used as a complete product until they have no proper values. For example, an email contains the sender address, recipient address, subject, content, and appendix. However, this email cannot be sent before the minimum recipient address is assigned a value.
In some cases, some properties of an object must be assigned values in a certain order to make sense. Before a property is assigned a value, another property cannot be assigned a value. These situations make the construction of nature involve complex business logic. At this time, this object is equivalent to a product to be built, and these properties are equivalent to the parts of the product, the process of building the product is the process of building parts. Due to the complex process of building parts, the construction process of these parts is often "Externalized" to another object called builder, the builder object returned to the client is a product object where all parts have been built.
The construction model creates all the parts one by one using a director object and a specific builder object to create a complete product object. The builder mode hides the structure of the product and the construction process of the parts of the product from the client, and separates the responsibility for directing the construction process from the responsibility of the specific parts of the builder, achieve responsibility division and encapsulation.
Construction mode structure
In this schematic system, the final product has only two parts, namely, Part1 and Part2. There are two corresponding construction methods: buildpart1 () and buildpart2 (). At the same time, we can see that this mode involves four roles:
Abstract builder role:An abstract interface is provided to standardize the construction of each component of the product object. Generally, this interface is independent of the application.ProgramBusiness logic. The concretebuilder role is used to directly create a product object. The specific builder class must implement the two methods required by this interface: one is the construction method (buildpart1 and buildpart2), and the other is the return structure method (retrieveresult ). In general, the number of parts contained in the product is consistent with the number of construction methods. In other words, how many parts are there is a corresponding construction method.
Specific builder role:Classes that are closely related to applications are used to create product instances under application calls. The tasks to be completed by this role include: 1. Implement the interface declared by the abstract Builder, and provide step-by-step operations for creating product instances. 2. Provide product instances after the construction is completed.
Ctor role:The class that assumes this role calls the specific builder role to create a product object. It should be pointed out that the director role does not have specific knowledge of the product class, and the specific knowledge of the product class is actually the specific builder role.
Product role:Products are complex objects during construction. Generally, a system has more than one product class, and these product classes do not necessarily have a common interface, but they can be completely unrelated.
The Director role is a role that deals with clients. The Director divides the client product creation request into the construction request for each part, and then delegates the request to the specific builder role. The specific builder role is used for specific construction, but is not known to the client.
Generally, each product class has a specific builder class. These products should have the same number of parts, and each part has a corresponding construction method in all builder roles.
Source code
Product
Public Class Product {
/**
* Define product operations
*/
Private String Part1;
Private String Part2;
Public String getpart1 (){
Return Part1;
}
Public Void Setpart1 (string Part1 ){
This . Part1 = Part1;
}
Public String getpart2 (){
Return Part2;
}
Public Void Setpart2 (string Part2 ){
This . Part2 = Part2;
}
}
Abstract builder class Builder
Public InterfaceBuilder {
Public VoidBuildpart1 ();
Public VoidBuildpart2 ();
PublicProduct retrieveresult ();
}
Concretebuilder
Public Class Concretebuilder Implements Builder {
Private Product = New Product ();
/**
* Product part construction method 1
*/
@ Override
Public Void Buildpart1 (){
// Build the first part of the product
Product. setpart1 ("ID: 9527 ");
}
/**
* Product part construction method 2
*/
@ Override
Public Void Buildpart2 (){
// Build the second part of the product
Product. setpart2 ("Name: XXX ");
}
/**
* Product return Method
*/
@ Override
Public Product retrieveresult (){
Return Product;
}
}
Director class ctor
Public Class Director {
/**
* Hold the builder object to be used currently
*/
Private Builder;
/**
* Constructor
* @ Param Builder object
*/
Public Director (Builder ){
This . Builder = builder;
}
/**
* Product construction method, responsible for calling the construction methods of each part
*/
Public Void Construct (){
Builder. buildpart1 ();
Builder. buildpart2 ();
}
}
Client
Public ClassClient {
Public Static VoidMain (string [] ARGs ){
Builder =NewConcretebuilder ();
Director dire=NewDirector (builder );
Director. Construct ();
Product = builder. retrieveresult ();
System. Out. println (product. getpart1 ());
System. Out. println (product. getpart2 ());
}
}
Sequence Chart
The client is responsible for creating directors and specific builder objects. Then, the client handed over the specific builder object to the Director. The director operated on the specific builder and started to create the product. After the product is completed, the builder returns the product to the client.
The task of creating a specific builder object is handed over to the client rather than the Director object, in order to change the coupling between the Director object and the specific builder object to dynamic, this allows the Director object to manipulate any of several specific builder objects.
Use Cases
Suppose there is an e-Magazine system that regularly sends e-magazines to users' email mailboxes. You can subscribe to an electronic magazine through the web page or through the web page. When the customer starts to subscribe, the system sends an email to send a greeting. When the customer ends the subscription, the system sends an email to send a greeting. In this example, the system is responsible for sending "welcome" and "farewell" emails.
In this example, the product category is the "welcome" and "farewell" emails sent to a customer, as shown in.
Although each product class in this example has a common interface, this is only unique in this example and does not represent the characteristics of the construction mode. The build mode can be applied to product classes with completely different interfaces. In most cases, I don't know what the final product is. Therefore, in the standard construction mode, it is generally not necessary to define abstract interfaces for the product, because the final products are very different, defining public interfaces for these products is almost meaningless.
This is the class diagram of the system.