创造型模式-生成器模式

生成器模式:能够从简单的对象一步一步生成复杂的对象,生成器模式是一种用来逐步构建复杂对象并在最后一步返回对象的创造型模式。构造一个对象的过程是通过泛型实现的,以便它能够用于对同一个对象创建不同的表示形式。

【产品抽象类】

package create.builder;

/**
 * CarPlan抽象产品类
 * Created by admin on 2018/11/26.
 */
public interface CarPlan {

    public void setBase(String basement);
    public void setWheels(String structure);

    public void setEngine(String structure);

    public void setRoof(String structure);

    public void setMirrors(String roof);

    public void setLights(String roof);

    public void setInterior(String interior);

}

package create.builder;

/**
* Created by admin on 2018/11/26.
*/
public class Car implements CarPlan {

private String base;
private String wheels;
private String engine;
private String roof;
private String mirrors;
private String lights;
private String interior;



public void setBase(String basement) {
this.base = basement;
}

public void setWheels(String structure) {

this.wheels = structure;

}

public void setEngine(String structure) {

this.engine = structure;

}

public void setRoof(String structure) {

this.roof = structure;

}

public void setMirrors(String roof) {

this.mirrors = roof;

}

public void setLights(String roof) {

this.lights = roof;

}

public void setInterior(String interior) {

this.interior = interior;

}
}
 

【抽象builder】

package create.builder;

/**
 * Created by admin on 2018/11/26.
 */
public interface CarBuilder {

    public void builderBase();

    public void builderWheels();

    public void builderEngine();

    public void builderRoof();

    public void builderMirrors();

    public void builderLihts();

    public void builderInterior();

    public Car getCar();

}

【具体Builder】

package create.builder;

/**
 * Created by admin on 2018/11/26.
 */
public class HighEndCarBuilder implements CarBuilder {

    private Car car;

    public HighEndCarBuilder(){
        car = new Car();
    }

    public void builderBase() {
        car.setBase("Quality base");
    }

    public void builderWheels() {

        car.setWheels("Quality Tyres");

    }

    public void builderEngine() {

        car.setEngine("High-end Engine");

    }

    public void builderRoof() {

        car.setRoof("Flexible roof");

    }

    public void builderMirrors() {

        car.setMirrors("Quality Mirrors");

    }

    public void builderLihts() {

        car.setLights("Quality Lights");

    }

    public void builderInterior() {

        car.setInterior("High-end Interior");

    }

    public Car getCar() {
        return this.car;
    }
}

【具体Builder】

package create.builder;

/**
 * Created by admin on 2018/11/26.
 */
public class LowPriceCarBuilder implements CarBuilder {

    private Car car;

    public LowPriceCarBuilder(){
        car = new Car();
    }

    public void builderBase() {
        car.setBase("Low priced base");
    }

    public void builderWheels() {
        car.setWheels("Cheap Tyres");
    }

    public void builderEngine() {
        car.setEngine("Low Quality Engine");
    }

    public void builderRoof() {
        car.setRoof("No flexible roof");
    }

    public void builderMirrors() {
        car.setMirrors("Cheap Mirrors");
    }

    public void builderLihts() {
        car.setLights("Cheap Lights");
    }

    public void builderInterior() {
        car.setInterior("Cheap Interior");
    }

    public Car getCar() {
        return this.car;
    }
}

【导演类】

package create.builder;

/**
 * 导演类构造Car对象
 * Created by admin on 2018/11/27.
 */
public class Mechanicalengineer {

    private CarBuilder carBuilder;

    public Mechanicalengineer(CarBuilder carBuilder){
        this.carBuilder = carBuilder;
    }

    public Car getCar(){
        return carBuilder.getCar();
    }

    public void builderCar(){
        carBuilder.builderBase();
        carBuilder.builderWheels();
        carBuilder.builderEngine();
        carBuilder.builderRoof();
        carBuilder.builderMirrors();
        carBuilder.builderLihts();
        carBuilder.builderInterior();
    }

}

【测试类】

package create.builder;

/**
 * Created by admin on 2018/11/27.
 */
public class TestBuilder {

    public static void main(String[] args) {
        CarBuilder lowPriceCarBuilder = new LowPriceCarBuilder();
        Mechanicalengineer mechanicalengineer = new Mechanicalengineer(lowPriceCarBuilder);
        mechanicalengineer.builderCar();
        Car car = mechanicalengineer.getCar();
        System.out.println("Builder Constructed Car:"+car);
    }

}

 

posted on 2018-11-26 19:26  airycode  阅读(208)  评论(0编辑  收藏  举报

导航