Matrix42

不饱食以终日,不弃功于寸阴

GOF23设计模式之 : 建造者模式(四)

场景

  • 我们要建造一个复杂的产品.比如:神州飞船,iphone.这个复杂的产品的创建.有这样一个问题需要处理
    • 装配这些子组件是不是有个步骤问题?
  • 实际开发中,我们所需要的对象构建时,也非常复杂,有很多步骤需要处理时

建造模式的本质

  • 分离了对象子组件的单独构造(由Builder来负责)和装配(由Director负责),从而可以构造出复杂的对象,这个模式适用于:某个对象的构建过程复杂的情况

  • 由于实现了构建和装配的解耦,不同的构建器,相同的装配,也可以做出不同的对象相同的构建器,不同的装配顺序也可以做出不同的对象,也就是实现了构建算法、装配算法的解耦,实现了更好的复用

public class AirShip {
    private OrbitalModule orbitalModule;   //轨道舱
    private Engine engine;    //发动机
    private EscapeTower escapeTower;     //逃逸塔
    public OrbitalModule getOrbitalModule() {
        return orbitalModule;
    }

    public void Launch(){
        System.out.println("发射!");
    }

    public void setOrbitalModule(OrbitalModule orbitalModule) {
        this.orbitalModule = orbitalModule;
    }
    public Engine getEngine() {
        return engine;
    }
    public void setEngine(Engine engine) {
        this.engine = engine;
    }
    public EscapeTower getEscapeTower() {
        return escapeTower;
    }
    public void setEscapeTower(EscapeTower escapeTower) {
        this.escapeTower = escapeTower;
    }

}

class OrbitalModule{
    private String name;
    public OrbitalModule() {
    }
    public OrbitalModule(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

class Engine{
    private String name;
    public Engine() {
    }
    public Engine(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

class EscapeTower{
    private String name;

    public EscapeTower() {
        super();
    }

    public EscapeTower(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public interface AirShipBuilder {
    Engine buildEngine();
    OrbitalModule buildOrbitalModule();
    EscapeTower buildEscapeTower();
}
public class LorindaArishipBuilder implements AirShipBuilder{

    @Override
    public Engine buildEngine() {
        System.out.println("构建发动机!");
        return new Engine("Lorinda发动机");
    }

    @Override
    public OrbitalModule buildOrbitalModule() {
        System.out.println("构建轨道舱!");
        return new OrbitalModule("Lorinda轨道舱");
    }

    @Override
    public EscapeTower buildEscapeTower() {
        System.out.println("构建逃逸塔!");
        return new EscapeTower("Lorinda逃逸塔");
    }

}
public interface AirShipDirector {
    AirShip createAirShip();
}
public class LorindaAirShipDirector implements AirShipDirector{

    private AirShipBuilder builder;

    public LorindaAirShipDirector(AirShipBuilder builder) {
        super();
        this.builder = builder;
    }

    @Override
    public AirShip createAirShip() {
        Engine engine = builder.buildEngine();
        OrbitalModule module = builder.buildOrbitalModule();
        EscapeTower tower = builder.buildEscapeTower();
        AirShip ship = new AirShip();
        ship.setEngine(engine);
        ship.setEscapeTower(tower);
        ship.setOrbitalModule(module);
        return ship;
    }

}
public class Client {
    public static void main(String[] args) {
        AirShipDirector director = new LorindaAirShipDirector(new LorindaArishipBuilder());
        AirShip ship = director.createAirShip();

        System.out.println(ship.getEngine().getName());

        ship.Launch();
    }
}

开发中的应用场景

  • StringBuilder类的append方法

  • SQL中的PreparedStatement

  • JDOM中,DOMBuilder,SAXBuilder

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

2 − 2 =