⑥ 设计模式的艺术-06.建造者(Builder)模式

场景

我们要建造一个复杂的产品。比如:神州飞船,Iphone。这个复杂的产品的创建。有这样一个问题需要处理:

装配这些子组件是不是有个步骤问题?

实际开发中,我们所需要的对象构建时,也非常复杂,有很多步骤需要处理时。

建造模式的本质

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

由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。

也就是实现了构建算法、装配算法的解耦,实现了更好的复用。

 

代码示例

AirShip.java

public class AirShip {
    
    private OrbitalModule orbitalModule;//轨道舱
    private Engine engine;//发动机
    private EscapeTower escapeTower;//逃逸塔
    
    public void launch(){
        System.out.println("发射!");
    }
    
    public OrbitalModule getOrbitalModule() {
        return orbitalModule;
    }
    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(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Engine {
    private String name;

    public Engine(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    
}

class EscapeTower{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

AirShipBuilder.java

public interface AirShipBuilder {
    Engine builderEngine();
    OrbitalModule builderOrbitalModule();
    EscapeTower  builderEscapeTower();
}

AirShipDirector.java

public interface AirShipDirector {
    /**
     * 组装飞船对象
     * @return
     */
    AirShip   directAirShip();
}

SxtAirShipBuilder.java

public class SxtAirShipBuilder implements AirShipBuilder {
    //StringBuilder, 以后学习XML解析中,JDOM库中的类:DomBuilder,SaxBuilder
    @Override
    public Engine builderEngine() {
        System.out.println("构建发动机!");
        return new Engine("发动机!");
    }

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

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

SxtAirshipDirector.java

public class SxtAirshipDirector implements AirShipDirector {

    private AirShipBuilder builder;

    public SxtAirshipDirector(AirShipBuilder builder) {
        this.builder = builder;
    }

    @Override
    public AirShip directAirShip() {
        Engine e = builder.builderEngine();
        OrbitalModule o = builder.builderOrbitalModule();
        EscapeTower et = builder.builderEscapeTower();
        
        //装配成飞船对象
        AirShip ship = new AirShip();
        ship.setEngine(e);
        ship.setEscapeTower(et);
        ship.setOrbitalModule(o);
        
        return ship;
    }

}

Client.java

public class Client {
    public static void main(String[] args) {
        
        AirShipDirector director = new SxtAirshipDirector(new SxtAirShipBuilder());

        AirShip ship = director.directAirShip();
        
        System.out.println(ship.getEngine().getName());
        
        ship.launch();
    }
}

开发中应用场景

StringBuilder类的append方法

SQL中的PreparedStatement

JDOM中,DomBuilder、SAXBuilder

 

posted @ 2018-03-22 20:34  ~沐风  阅读(198)  评论(0编辑  收藏  举报