1. Concept of builders ' models
definition: Separating a complex object construct from its presentation so that the same build process can create different representations;
Core: Separation of construction and presentation, different representations of construction
Different from abstract Factory mode:
(1) is similar to the abstract factory pattern because it can also create complex objects. The main difference is that the builder model focuses on the step-by-step construction of a complex object, focusing on the part type and the order of the Assembly process. The abstract factory model focuses on multiple series of product objects (simple or complex). The builder pattern returns the product in the final step, and for the abstract factory, the product is returned immediately.
(2) in the builder model, there is a mentor who manages the builder, the user is contacted by the mentor, and the mentor contacts the builder to get the product. That is, the construction model can impose a step-by-step construction process.
2. Its role
(1) the Abstract Builder (Builder) role: This role is used to standardize the components of a product and to abstract, generally independent of application logic.
(2) Specific builder (ConcreteBuilder) role:
This role implements all the methods defined in the abstract builder and returns a set of good product instances.
(3) Product role:
The role is a complex object in construction, and there will be more than one product class in a system that does not necessarily have a public interface and can be completely unrelated.
(4) Role of Mentor (Director):
The role is responsible for arranging the order of the existing modules and then telling the builder Builder to start building.
3. Examples
Public interface Builder {
void Buildparta ();
void Buildpartb ();
void Buildpartc ();
}
public class Builderimpl implements Builder {
@Override public
void Buildparta () {
System.out.println (" Build Part A ");
@Override public
void Buildpartb () {
System.out.println ("Build part B");
}
@Override public
void Buildpartc () {
System.out.println ("Build part C");
}
public class BuilderImpl2 implements Builder {
@Override public
void Buildparta () {
System.out.println ( "Building part AA");
@Override public
void Buildpartb () {
System.out.println ("Build part BB");
}
@Override public
void Buildpartc () {
System.out.println ("Build part cc");
}
/**
* Conductor: Guide How to build
* Combination Builder */public
class Director {
private Builder Builder;
Public Director (Builder Builder) {
this.builder = Builder;
}
/**
* Construction Method: Defines the construction process
* If additional processes are required to implement, then a new conductor director can be *
* * public
void construct () {
SYSTEM.OUT.PRINTLN ("Director Command Builder for Construction");
Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
}
public class Director2 {
private Builder Builder;
Public Director2 (Builder Builder) {
this.builder = Builder;
}
/**
* Construction Method: Defines the construction process
* If additional processes are required to implement, then a new conductor director can be *
* * public
void construct () {
System.out.println ("Director2 Command builder for Construction");
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
Builder.buildparta ();
}
public class Test {public
static void Main (string[] args) {
Builder Builder = new Builderimpl ();
Director Director = new Director (builder);
* * The same build process, different modules (Buildparta, BUILDPARTB, BUILDPARTC) implementation, new one builder implementation
* Different build process, new one director
* Different construction process, different module implementation, new Director,new builder
* * *
director.construct ();
System.out.println ("");
Builder builder2 = new BuilderImpl2 ();
Director Director2 = new Director (builder2);
Director2.construct ();
System.out.println ("");
Builder Builder3 = new BuilderImpl2 ();
Director2 Director3 = new Director2 (BUILDER3);
Director3.construct ();
}
Print:
Director Command Builder carry out construction parts
a
building part B
Building parts C
Director Command Builder carry out building
parts AA
Building Parts BB
build Parts cc
DIRECTOR2 command builder carry out construction
components BB
build parts cc
build parts AA
4. Advantages and Disadvantages
(1) Advantages:
A. Allows you to change the internal representation of the product.
B. Package architecture and Representative code.
C. Provide control over the steps of the construction process.
(2) Disadvantages:
A. The need to create a variety of different types of products separate concretebuilder.
5. Use scene:
(1) When creating complex objects the algorithm should be independent of the components of the object and the way they are assembled.
(2) When the construction process must allow the constructed object to have a different representation (the same method, different execution order, produce different results).