Builder mode
In this article, I will use the production of the car example to explain the Builder mode.
Consider the following questions: I want to build a car, we want to separate this complex object to it, so that the same build process can create different representations. For example, Santana, Passat and other cars, the same object can be constructed in the same construct.
Compare Abstract Factory and Builder Different points are: (1) Applicable issues (2) Build mode.
Builder is a complex object; (applicable question) Step by step and eventually returns a product; (build mode)
Abstract Factory is focused on multiple series of products (a complex object); (applicable issues) The product is immediately returned (build mode)
As mentioned above, the following issues can be used in Builder mode.
[When the algorithm for creating complex objects should be independent of the component of this object and their assembly method]: Consider our example: Workshop will be equipped with Santana sedan, we can assemble the car products step by step.
[When the structure is allowed to be constructed, there is a different representation]: If we need to assemble Santana sedan now to make a slight change in accordance with the market sector, such as: change a certain design, then launch a new product;
Let us know the structure of the Builder mode:
We can see its participants include: Builder: Specify an abstract interface for each part of the product object; CreateEnginee (), CreateEL ()) ConcerteBuilder (STNCARBUILDER): Implement Builder's interface to create and Part 10; CreateEnginee (): CreateEng (): Definition and explicitly creation indicating: Provide an interface for retrieving products (Getstncar ())
Director: Constructs an object product (STNCAR) using the Builder interface: Indicates complex objects that are constructed: including classes that define the composition of the product, including the interface to assemble these parts into the final product
Let us first realize the definition of the product object: We define a car parent class: Car (this is not required) ZZ as an interface of the product, a sub-class STNCAR to achieve true products;
Class car {car () {} void addwindow {} void address {} void addenginee {} void Runs () {}
class StnCar extends Car {void addWindow (Windows _window) {} void addWheel (Wheel _wheel) {} void addEnginee (Enginee _enginee) {} StnCar () {} void Runs () {System.out.println ( "stncar is ok" }}
Next, the aggregate components of the product are defined. We define the parts of Santana car as follows: Class Windows {Windows () {}}
Class wheel {wheel () {}}
Class Enginee {engineee () {}}
Next, let us define Builder and its implementation Builder: STNCARBUILDER. They are responsible for combining the products we need.
Public class testaaa {
Class Builder {Void CreateWindow () {} void createngu () {} void createEl () {} stncar getstncar ()} stncar getstncar ()} retint
class StnCarBuilder extends Builder {private StnCar _stncar; void StnCarBuilder () {_stncar = null;} void CreateWindow () {Windows _window = new Windows (); _stncar.addWindow (_window);} void CreateEnginee () {Enginee _enginee = new Enginee (); _Stncar.addengineee (_Enginee);} void CreateWheel () {WHEEL _WHEEL = New wheel (); _stncar.addwheel (_wheel);} void createstacar () {_stncar = new stncar ();} stncar getstncar () Return_stncar;}}
We now define the instructor Director using the Builder interface: Class Director {
Car createProduct (Builder Buildu) {Builder.createStacar (); builder.createWindow (); builder.createEngineeE (); builder.createEL (); return builder.getstncar ();
}
Finally, we test whether our products have been produced. Note that the CAR class is used. We can no longer specify specific products and achieve some advantages of Abstract Factory. public static void main (String [] args) {Director _d = new Director (); Car _car = null; StnCarBuilder _stncarbuilder = new StnCarBuilder (); _car = _d.CreateProduct (_stncarbuilder); _car.Runs ();}
}