当我们要构造的对象非常复杂,即对象中的组件需要有顺序的被构建时,就应该使用建造者模式。
实际开发中,建造者模式通常用来构造复杂对象。建造者模式分离了对象子组件的构造(Build)和装配(Direct),构造由Builder负责,装配有Director负责。每个组件由Builder单独构建,然后再交给Director组装,从而可以构造出非常复杂的对象。
由于实现了构造和装配的解耦,不同的Builder,相同的Director,也可以造出不同对象。相同的Builder,不同的Director也可以做出不同的对象。也就是实现了构造算法、装配算法的解耦,实现了更好的复用。
建造者模式UML如下图所示:
组件类定义:
1 2 3 4 5 6 7 8 9 10 11 12
| public class OrbitalModule{ 4private String name; 4public OrbitalModule(String name) { 44this.name = name; 4} 4public String getName() { 44return name; 4} 4public void setName(String name) { 44this.name = name; 4} }
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class Engine { 4private String name; 4public Engine(String name) { 44this.name = name; 4} 4public String getName() { 44return name; 4} 4public void setName(String name) { 44this.name = name; 4} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| class EscapeTower{ 4private String name; 4public String getName() { 44return name; 4} 4public void setName(String name) { 44this.name = name; 4} 4public EscapeTower(String name) { 44super(); 44this.name = name; 4} }
|
飞船类定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class AirShip { 4private OrbitalModule orbitalModule; 4private Engine engine; 4private EscapeTower escapeTower; 4 public void lunch(){} 4public OrbitalModule getOrbitalModule() { 44return orbitalModule; 4} 4public void setOrbitalModule(OrbitalModule orbitalModule) { 44this.orbitalModule = orbitalModule; 4} 4public Engine getEngine() { 44return engine; 4} 4public void setEngine(Engine engine) { 44this.engine = engine; 4} 4public EscapeTower getEscapeTower() { 44return escapeTower; 4} 4public void setEscapeTower(EscapeTower escapeTower) { 44this.escapeTower = escapeTower; 4} }
|
Builder接口:
1 2 3 4 5
| public interface AirShipBuilder { 4Engine builderEngine(); 4OrbitalModule builderOrbitalModule(); 4EscapeTower builderEscapeTower(); }
|
Builder实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class MyAirShipBuilder implements AirShipBuilder { 4@Override 4public Engine builderEngine() { 44return new Engine("MyEngine"); 4} 4@Override 4public EscapeTower builderEscapeTower() { 44return new EscapeTower("MyEscapeTower"); 4} 4@Override 4public OrbitalModule builderOrbitalModule() { 44return new OrbitalModule("MyOrbitalModule"); 4} }
|
Director接口:
1 2 3
| public interface AirShipDirector { 4AirShip directAirShip(); }
|
Director实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class MyAirshipDirector implements AirShipDirector { 4private AirShipBuilder builder; 4public MyAirshipDirector(AirShipBuilder builder) { 44this.builder = builder; 4} 4@Override 4public AirShip directAirShip() { 44Engine e = builder.builderEngine(); 44OrbitalModule o = builder.builderOrbitalModule(); 44EscapeTower et = builder.builderEscapeTower(); 44AirShip ship = new AirShip(); 44ship.setEngine(e); 44ship.setEscapeTower(et); 44ship.setOrbitalModule(o); 44return ship; 4} }
|
Client:
1 2 3 4 5 6 7 8
| public class Client { 4public static void main(String[] args) { 44AirShipDirector director = new SxtAirshipDirector(new SxtAirShipBuilder()); 44AirShip ship = director.directAirShip(); 44 ship.launch(); 4} }
|
典型应用场景:
- StringBuilder类的append方法
- SQL中的PreparedStatement
- JDOM中的DomBuilder, SAXBuilder