First, Builder Model Introduction
Builder mode: separates a complex build from its presentation, allowing the same build process to create different representations. [Construction and presentation separation, different representations of construction]
If the construction of an object is complex, it takes a lot of steps. You can use the Builder mode to separate the two steps of building objects and assembling them into an object. The build section (builder) and the organizational section (Director) enable decoupling of the build and assembly.
Different builders, the same assembly can also make different objects.
The same builder, different assembly order can also make different objects.
For example: To produce a car now, there will be a lot of parts, and the parts need to be assembled into a complete car. The builder mode is to separate the build part (builder) from the two operations of the Assembly part (director).
The difference from the abstract factory : In the builder model, there is a mentor who manages the builder, the user is in contact with the mentor, and the mentor contacts the builder to get the product. That is, the construction model can enforce a process of construction in stages.
Second, the Code implementation
Implementation of the Code: the realization of the spaceship
1, if now to build a Spaceship object, first define a spaceship object, there are some simple properties.
123456789101112131415161718192021222324 |
/**
* 宇宙飞船
*/
public class AirShip {
private OrbitalModule orbitalModule;
//轨道舱
private Engine engine;
//发动机
private EscapeTower escapeTower;
//逃逸塔
//省略get,set,构造器
}
//轨道舱
class OrbitalModule{
private String name;
//省略get,set,构造器
}
//发动机
class Engine{
private String name;
//省略get,set,构造器
}
//逃逸塔
class EscapeTower{
private String name;
//省略get,set,构造器
}
|
2. With the above attributes, you can then build up some parts of the spaceship. In order to build different types of spacecraft later, the interface is used to define the build. What kind of ship do you have?
Class to implement the interface.
12345678 |
/**   * build Interface: The interface used to build components in Airshipz   */ public Code class= "Java keyword" >interface airshipbuilder {      engine builderengine (); //build engine      orbitalmodule builderorbitalmodule (); //building the orbital compartment      escapetower builderescapetower (); //Building escape Tower |
3, after the construction of these parts, it is necessary to build these components into a complete spaceship (AirShip). Using interfaces to define, different types of spacecraft assembly can be implemented in different ways.
12345678910 |
/**   * assembly interface: Used to assemble ship components */ public interface airshipdirector { /** * Assembly spaceship * @return */ airship directorairship (); |
4. Begin to truly realize the construction of the spacecraft and the Assembly of the spacecraft
4.1, the implementation of the spacecraft construction
1234567891011121314151617181920 |
/**
* 具体飞船的构造
*/
public class FzAirShipBuilder
implements AirShipBuilder{
@Override
public Engine builderEngine() {
System.out.println(
"万能牌发动机"
);
return new Engine(
"万能牌发动机"
);
}
@Override
public OrbitalModule builderOrbitalModule() {
System.out.println(
"万能牌轨道舱"
);
return new OrbitalModule(
"万能牌轨道舱"
);
}
@Override
public EscapeTower builderEscapeTower() {
System.out.println(
"万能牌逃逸塔"
);
return new EscapeTower(
"万能牌逃逸塔"
);
}
}
|
4.2, the specific spacecraft assembly implementation: it is important to note that the assembly needs to be built- up (the complete assembly of the spacecraft requires the aircraft's components to be built successfully to assemble)
12345678910111213141516171819202122232425 |
/**
* 具体飞船的组装
*/
public class FzAirShipDirector
implements AirShipDirector{
private AirShipBuilder builder;
//创建构建者的引用
public FzAirShipDirector(AirShipBuilder airShipBuilder) {
this
.builder = airShipBuilder;
}
/**
* 组装具体的对象,为了简单,这里的组装步骤比较简单。实际产品中较复杂
*/
@Override
public AirShip directorAirShip() {
Engine e = builder.builderEngine();
//构建发动机
EscapeTower et = builder.builderEscapeTower();
//构建逃逸塔
OrbitalModule o = builder.builderOrbitalModule();
//构建轨道舱
//装配对象
AirShip ship =
new AirShip();
ship.setEngine(e);
ship.setEscapeTower(et);
ship.setOrbitalModule(o);
return ship;
}
}
|
5, in the specific use of our customers, only need to know the construction interface, assembly interface and a spaceship object, we do not need to know how to assemble the internal, how many parts inside.
Just know how to use the assembly.
12345678 |
public static void main(String[] args) {
AirShipDirector shipDirector =
new FzAirShipDirector(
new FzAirShipBuilder());
AirShip airShip = shipDirector.directorAirShip();
//构建对象
// System.out.println(airShip.getEngine());
}
|
The results are as follows:
Universal Brand Engine
Universal Card Escape Tower
Universal Card Orbital module
Third, the builder mode application scenario:
If the construction of an object is complex and requires a lot of steps, consider using the builder pattern to separate the construction of the object from the final assembly.
In a class that is normally used, if the suffix is builder, the builder mode
The Apend method of StringBuilder class
PreparedStatement in SQL
The Dombuilder and Saxbuilder in Jdom
From for notes (Wiz)
Builder mode (builders and director)