This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
preface
We can create an object, through new and different parameters can be by defining method in the construction of the implementation to create the effect of different objects, but the consequence of this approach is, as the business is complex, construction method will be more and more, the number of parameters will be more and more, resulting in maintenance difficulties, understanding the harder. The builder pattern solves this problem.
define
Separate the construction and presentation of a complex object, and the same construction process can create different representations.
Splitting a complex object into simple objects and then combining them together to form a complete object representation, the simple objects can be flexibly selected, resulting in the same construction process, creating a representation that can represent as many different results as desired
advantages
- Good encapsulation, build and presentation separation
- The decoupling
- The builder refines the creation process, and the client does not have to know the internal composition details of the product
disadvantages
- The components of the product must be the same
- If there are changes inside the product, the builder has to synchronize them, which is costly to maintain
In general, it seems that the factory pattern also takes object creation out of the picture, but the Builder pattern and the factory pattern do have a very different focus: the Builder pattern focuses on the assembly process of parts, while the factory pattern focuses on the creation process of parts, but the two can be used together.
The illustration
implementation
Complex product Object: The Iphone contains a complex object with multiple components
@Data
public class Iphone {
private Battery battery;/ / battery
private Screen screen; / / display
/* Display product features */
public void show(a) {
battery.print();
screen.print();
System.out.println("IPhone is assembled."); }}Copy the code
Component A: Battery
public class Battery {
public void print(a) {
System.out.println("Battery production completed"); }}Copy the code
Component B: Specific product: Screen
public class Screen {
public void print(a) {
System.out.println("Display screen production completed"); }}Copy the code
Abstract Builder: A Builder contains an abstract method for creating multiple parts, often with a concrete method that returns a complex object
abstract class Builder {
// Create the product object
protected Iphone iphone = new Iphone();
public abstract void buildBattery(a);
public abstract void buildScreen(a);
// Return the product
public Iphone getResult(a){
returniphone; }}Copy the code
Concrete builder: BuilderImpl completes the concrete method of creating the components of a complex part
// Concrete builder: implements the abstract Builder interface.
public class BuilderImpl extends Builder {
@Override
public void buildBattery(a) {
iphone.setBattery(new Battery());
}
@Override
public void buildScreen(a) {
iphone.setScreen(newScreen()); }}Copy the code
The Director specifies the specific way of production and assembly
public class Director {
private final Builder builder;
public Director(Builder builder){
this.builder = builder;
}
// Product assembly method
public Iphone construct(a){
builder.buildBattery();
builder.buildScreen();
returnbuilder.getResult(); }}Copy the code
Client:
public class Client {
public static void main(String[] args) {
Builder builder = new BuilderImpl();
Director director = newDirector(builder); Iphone iphone = director.construct(); iphone.show(); }}Copy the code
A printout
The battery is manufactured and the display is manufactured and the iPhone is assembledCopy the code
instructions
- When it’s time to produce one
Iphone
When the client (Client
) Direct and commanding (Director
) to conduct demand communication;- After the pass
Director
Will produceIphone
Is distributed to the builder of each component (Builder
);- Building requests for each component are delegated to the specific builder (
BuilderImpl
);- Each individual builder is responsible for the construction of product components;
- The final structure is constructed into a complete
Iphone
.
When building specific components, we can combine the factory mode to produce components from different manufacturers, and finally get iphones with different configurations. Friends who are interested here can realize it by themselves.
conclusion
- In your work, learn to use the builder pattern if you need to separate the construction of a complex object from its representation or if you encounter multiple constructor parameters.
- The builder pattern focuses more on the order in which objects are created. A complex object behaves differently through different assembly orders. The factory pattern focuses on the creation of objects, and the objects created are the same.