GOF23设计模式之 建造者模式

场景:

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

建造模式的本质:

  • 分离了对象子组件的单独构造(由Builder来负责)和装配(由Derector负责)。从而可以构造出复杂的对象。这个模式适用于:某个对象的构建过程伏在的情况下使用。
  • 由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的解构器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法、装配算法的解耦,实现了更好的复用。

开发中应用场景

  • StringBuilder的append方法
  • SQL 中的 PreparedStatement 方法
  • JDOM 中,DomBuilder、SAXBuilder

代码示例

  • AirShipBuilder
package com.coderbean.builder;
/**
 * 建造者接口
 * @author Chang
 *
 */
public interface AirShipBuilder {
    OrbitaModule builderOrbitaMoudule();
    Engine builderEngine();
    EscapeTower builerEscapeTower();

}
  • AirShipDirector
package com.coderbean.builder;
/**
 * 装配接口
 * @author Chang
 *
 */
public interface AirShipDirector {
    AirShip directAirShip();
}
  • MyAirShipBuilder
package com.coderbean.builder;

public class MyAirShipBuilder implements AirShipBuilder{

    @Override
    public OrbitaModule builderOrbitaMoudule() {
        System.out.println("构建我的轨道舱!");
        return new OrbitaModule("我的轨道舱");
    }

    @Override
    public Engine builderEngine() {
        System.out.println("构建我的发动机");
        return new Engine("我的发动机");
    }

    @Override
    public EscapeTower builerEscapeTower() {
        System.out.println("构建我的逃生舱");
        return new EscapeTower("我的逃生舱");
    }

}
  • MyAirShipDirector
package com.coderbean.builder;

public class MyAirShipDirector implements AirShipDirector {

    private AirShipBuilder builder;




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



    @Override
    public AirShip directAirShip() {
        Engine e = builder.builderEngine();
        OrbitaModule o = builder.builderOrbitaMoudule();
        EscapeTower et = builder.builerEscapeTower();

        AirShip ship = new AirShip();
        ship.setEngine(e);
        ship.setEscapeTower(et);
        ship.setOrbitaModule(o);

        return ship;
    }

}
  • AirShip
package com.coderbean.builder;
/**
 * 宇宙飞船
 * @author Chang
 *
 */
public class AirShip {
    private OrbitaModule orbitaModule; //轨道舱
    private Engine engine; //发动机
    private EscapeTower escapeTower; //逃逸舱

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

    public OrbitaModule getOrbitaModule() {
        return orbitaModule;
    }
    public void setOrbitaModule(OrbitaModule orbitaModule) {
        this.orbitaModule = orbitaModule;
    }
    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 OrbitaModule{
    private String name;



    public OrbitaModule(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 EscapeTower(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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


}


  • Client
package com.coderbean.builder;

public class Client {

    public static void main(String[] args) {
        AirShipBuilder asb = new MyAirShipBuilder();
        MyAirShipDirector asd = new MyAirShipDirector(asb);
        AirShip as = asd.directAirShip();
        as.luanch();
        System.out.println(as.getEngine());
    }

}

你可能感兴趣的:(Java)