The nature of the construction model:
--separate construction of the object subassembly (owned by builder) and assembly (by director). So that complex objects can be constructed. This pattern is intended for use in situations where the construction of an object is complex.
--the decoupling of construction and assembly is achieved. Different builders, the same assembly, can also make different objects, the same builder, different assembly order can also make different objects. It realizes the decoupling of construction algorithm and assembly algorithm, and realizes better reuse.
Cases:
1. Define the spacecraft class and engine, Orbital module, Escape Tower Category:
1 PackageCom.ztq.builder;2 3 Public classAirShip {4 PrivateOrbitalmodule Orbitalmodule;//Orbital Module5 PrivateEngine engine;//engine6 PrivateEscapetower Escapetower;//Escape Tower7 8 Public voidlaunch () {9SYSTEM.OUT.PRINTLN ("Launch! ");Ten } One Publicorbitalmodule Getorbitalmodule () { A returnOrbitalmodule; - } - Public voidsetorbitalmodule (Orbitalmodule orbitalmodule) { the This. Orbitalmodule =Orbitalmodule; - } - PublicEngine Getengine () { - returnengine; + } - Public voidSetengine (Engine engine) { + This. engine =engine; A } at Publicescapetower Getescapetower () { - returnEscapetower; - } - Public voidsetescapetower (Escapetower escapetower) { - This. Escapetower =Escapetower; - } in - } to + classorbitalmodule{ - PrivateString name; the Publicorbitalmodule (String name) { * This. Name =name; $ }Panax Notoginseng PublicString GetName () { - returnname; the } + A Public voidsetName (String name) { the This. Name =name; + } - } $ $ classengine{ - PrivateString name; - the PublicEngine (String name) { - This. Name =name;Wuyi } the - PublicString GetName () { Wu returnname; - } About $ Public voidsetName (String name) { - This. Name =name; - } - } A + classescapetower{ the PrivateString name; - $ Publicescapetower (String name) { the This. Name =name; the } the PublicString GetName () { the returnname; - } in the Public voidsetName (String name) { the This. Name =name; About } the}
2. Define the Airshipbuilder interface:
1 Public Interface Airshipbuilder {2 Engine builderengine (); 3 orbitalmodule builderorbitalmodule (); 4 escapetower builderescapetower (); 5 }
3. Define the Airshipdirector interface:
1 Public Interface airshipdirector {2 /** * 3 * Assemble Spaceship object 4 5 * / 6 AirShip directairship (); 7 }
4. Define the class that implements the Airshipbuilder interface:
1 Public classZtqairshipbuilderImplementsairshipbuilder{2 3 @Override4 PublicEngine Builderengine () {5System.out.println ("Build ZTQ brand engine! ");6 return NewEngine ("ZTQ-brand Engines");7 }8 9 @OverrideTen Publicorbitalmodule Builderorbitalmodule () { OneSystem.out.println ("Build ZTQ deck! "); A return NewOrbitalmodule ("ZTQ Rail module"); - } - the @Override - Publicescapetower Builderescapetower () { -System.out.println ("Build ZTQ card Escape tower!") "); - return NewEscapetower ("ZTQ Card Escape Tower"); + } -}
5. Define the class that implements the Airshipdirector interface:
1 Public classZtqairshipdirectorImplementsairshipdirector{2 3 PrivateAirshipbuilder Builder;4 5 Publicztqairshipdirector (Airshipbuilder builder) {6 This. Builder =Builder;7 }8 9 @OverrideTen PublicAirShip directairship () { OneEngine e =builder.builderengine (); AOrbitalmodule o =builder.builderorbitalmodule (); -Escapetower es =builder.builderescapetower (); - theAirShip ship =NewAirShip (); - Ship.setengine (e); - ship.setorbitalmodule (o); - Ship.setescapetower (es); + - returnShip ; + } A}
6. Define the test class:
1 Public classClient {2 Public Static voidMain (string[] args) {3Airshipdirector Director =NewZtqairshipdirector (NewZtqairshipbuilder ());4AirShip ship =director.directairship ();5 System.out.println (Ship.getengine (). GetName ());6 ship.launch ();7 }8}
Printing results:
Build ZTQ brand engine! Build the ZTQ deck! Build the ZTQ card escape tower! ZTQ Brand engine Launch!
UML diagram:
Design pattern (iii) builder mode