java设计模式【创建模式】之建造(Bulider)模式

来源:互联网 发布:剑灵天族捏脸数据大全 编辑:程序博客网 时间:2024/06/06 07:28

Builder模式:分步骤构建一个复杂对象,实现复杂对象的创建过程和对象的表示相分离.

 

不管在软件系统还是其他领域,有时候会遇到 一个复杂的工程或者对象的创建,而这个创建可能有各个子模块或对象用一定的逻辑或者算法组成,由于各种原因这个复杂工程或对象各个部分可能需要经常的更新或变化,但是整个组合体或者说这个工程或对象要保证足够的稳定性.【不会因为子模块变化造成对整个工程巨大影响】 --- this is Bulider's Application environment;

 

以车为例吧

 

假设

车 = 轮子 + 外壳 + 发动机

 

代码实现:

 

//车的部件生产和组装规范

public interface Builder {
   
    void makeWheel();
    void makeShell();
    void makeEngine();
    Car buildCar();
}

 

//车

public class Car {
    public Component getWheel() {
        return wheel;
    }
    public void setWheel(Component wheel) {
        this.wheel = wheel;
    }
    public Component getShell() {
        return shell;
    }
    public void setShell(Component shell) {
        this.shell = shell;
    }
    public Component getEngine() {
        return engine;
    }
    public void setEngine(Component engine) {
        this.engine = engine;
    }
    private Component wheel;
    private Component shell;
    private Component engine;
    public String toString(){return wheel.getColor()+":"+engine.getColor()+":"+shell.getColor();}
}

 

//车的组成部件

public class Component {
   
    private String color;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

 

//负责车的组装

public class CarFactory {
    private Builder builder;
    CarFactory(Builder builder){
        this.builder = builder;
    }
    public void buildCar(){
        builder.makeWheel();
        builder.makeEngine();
        builder.makeShell();   
    }
}

 

//负责宝马车的生产

public class BMWFactory implements Builder {

    Component wheel,shell,engine;
   
    public void makeWheel() {
        wheel = new Component();
        wheel.setColor("red");
    }

    public void makeShell() {
        shell = new Component();
        shell.setColor("magnificent");
    }

    public void makeEngine() {
        engine = new Component();
        engine.setColor("BMW");
    }

    public Car buildCar() {
        Car bmw = new Car();
        bmw.setEngine(engine);
        bmw.setShell(shell);
        bmw.setWheel(wheel);
        return bmw;
    }
   
}

 

//负责奥迪车的生产

public class AudiFactory implements Builder{

    Component wheel,shell,engine;
   
    public void makeWheel() {
        wheel = new Component();
        wheel.setColor("blue");
    }

    public void makeShell() {
        shell = new Component();
        shell.setColor("changpeng");
    }

    public void makeEngine() {
        engine = new Component();
        engine.setColor("dongfeng");
    }

    public Car buildCar() {
        Car audi = new Car();
        audi.setEngine(engine);
        audi.setShell(shell);
        audi.setWheel(wheel);
        return audi;
    }
}

 

//来买车吧

public class CarShop {

    private static Car get(Builder builder){
        CarFactory factory = new CarFactory(builder);
        factory.buildCar();
        return builder.buildCar();
    }
   
    /**
     * @param args
     */
    public static void main(String[] args) {
        AudiFactory audiFactory = new AudiFactory();
        BMWFactory bmwFactory = new BMWFactory();
        Car audi = get(audiFactory);
        System.out.println(audi);
        Car bmw = get(bmwFactory);
        System.out.println(bmw);

    }

}

 

这样不管车的部件不管变成什么样,你可以修改make***的内部逻辑,不会影响到 buildCar ..make***和buildCar 是分离的,

最后总能保证车的稳定生产.

 

原创粉丝点击