Core idea
The builter pattern is a creation design pattern that is used to build complex objects in steps. It allows users to customize the objects of the object through the process of controlling the object structure without directly instantiated their details. The builder mode is particularly suitable for building complex objects with a variety of configurations.
structure
- Builder (abstract builder)
Defines abstract methods for constructing products that concrete builders will implement. - ConcretBuilder (concrete builder)
Implement the Builder interface to define the specific process of constructing complex objects. - Director (commander)
Call the steps of the builder to control the construction process of the product. - Product (product)
The resulting complex object is constructed.
Applicable scenario
You need to create complex objects consisting of multiple parts that may need to be configured dynamically.
The constructor needs to be hidden to the outside world, but allows sub -steps to be constructed.
It is necessary to separate the construction process and representation of objects and provide different representation forms.
Advantages and Disadvantages
advantage:
Better control: Refine the construction process by building your product step-by-step.
Code reuse: Different products can be built through different concrete builders.
Decoupling: separation of construction and presentation.
shortcoming:
Increase complexity: need to define additional builders and commanders.
Not suitable for simple objects: For simple objects, it may seem too cumbersome.
Implement steps
Define a prototype of implementing the Cloneable interface.
Override the clone() method in the class to ensure the copy logic of the object.
Create a new object by calling the prototype's clone() method.
Precautions - Shallow copy and deep copy:
Shallow copy: Only the basic data type of the object is copied, and the reference type attributes still point to the original object.
Deep copy: Not only copy the basic type, but also copy the object of the reference type to create a completely independent copy. - Cloneable interface:
In Java, the Cloneable interface must be implemented and the clone () method must be rewritten, otherwise ClonenotsUpportedException will be thrown. - Not applicable scenarios:
For a highly dependent constructor or a framework that does not support Clone ().
Exemplary example
Expansion - chain call
package .creational_patterns.chain_builder;
public class Phone {
Private String CPU;
Private String Screen;
Private String Memory;
Private String Mainboard;
// Private construction method
Private Phone (Builder Builder) {
=;
=;
=;
=;
}
Public Static Final Class Builder {
Private String CPU;
Private String Screen;
Private String Memory;
Private String Mainboard;
Public Builder CPU (String Val) {
CPU = Val;
Return this;
}
Public Builder Screen (String Val) {
screen = value;
Return this;
}
Public Builder Memory (String Val) {
memory = value;
Return this;
}
Public Builder Mainboard (String Val) {
mainboard = value;
Return this;
}
// Use builder to create examples
public phone build () {
Return New Phone (this);
}
}
@Override
public string tostring () {
Return "Phone {" +
"CPU = '" + CPU +' \ '' +
"Screen = '" + Screen +' \ '' +
", memory = '" + memory +' \ '' +
", mainboard = '" + mainboard +' \ '' +
'}';
}
}
package .creational_patterns.chain_builder;
public class Client {
public static void main(String[] args) {
Phone phone = new ()
.cpu("Intel Core i7")
.memory("16GB")
.screen("13.3"")
.mainboard("Intel HM")
.build();
(phone);
}
}
The relationship with other models
In the early days of many design work, the factory method mode (simpler and more convenient to customize through subclasses) will be used, and then evolved into an abstract factory mode, prototype mode or generator mode (more flexible but more complicated).
Generators focus on how to generate complex objects step by step. Abstract factories are specifically used to produce a series of related objects. Abstract factories return products immediately, and generators allow you to perform some additional construction steps before getting the product.
You can use the generator when creating a complex combination mode tree, because this can operate its constructor steps recursion.
You can use a combination of generators and bridge patterns: the master class does the abstraction, and various generators do the implementation.
Abstract factories, generators, and prototypes can be implemented in a single example.