Builder模式

Posted 不停的奋斗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Builder模式相关的知识,希望对你有一定的参考价值。


/**抽象Product
* Created by marcopan on 17/10/23.
*/
public abstract class CarModel {
    private List<String> sequence = new ArrayList<String>();

protected abstract void start();
protected abstract void stop();
protected abstract void alarm();
protected abstract void engineBoom();

final public void run() {
for (int i = 0; i < this.sequence.size(); i ++) {
if ("start".equalsIgnoreCase(this.sequence.get(i))) {
this.start();
} else if ("stop".equalsIgnoreCase(this.sequence.get(i))) {
this.stop();
} else if ("alarm".equalsIgnoreCase(this.sequence.get(i))) {
this.alarm();
} else if ("engine boom".equalsIgnoreCase(this.sequence.get(i))) {
this.engineBoom();
}
}
}

final public void setSequence(List<String> sequence) {
this.sequence = sequence;
}

final public List<String> getSequence() {
return sequence;
}
}

/**抽象builder
* Created by marcopan on 17/10/23.
*/
public interface CarBuilder {
public abstract void setSequence(List<String> sequence);
//设置完毕顺序后,就可以直接拿到这个车辆模型了
public abstract CarModel getCarModel();
}

/**concrete builder
* Created by marcopan on 17/10/23.
*/
public class BMWBuilder implements CarBuilder {
private CarModel bmw = new BMWModel();

@Override
public void setSequence(List<String> sequence) {
this.bmw.setSequence(sequence);
}

@Override
public CarModel getCarModel() {
return this.bmw;
}
}

public class BenzBuilder implements CarBuilder {
private CarModel benz = new BenzModel(); //奔驰车模型

@Override
public void setSequence(List<String> sequence) {
this.benz.setSequence(sequence);
}

@Override
public CarModel getCarModel() {
return this.benz;
}
}

public class Director {
private List<String> sequence = new ArrayList<String>();
private CarBuilder benzBuilder = new BenzBuilder();
private CarBuilder bmwBuilder = new BMWBuilder();

//A顺序的奔驰车
public CarModel getABenzModel() {
this.sequence.clear();
this.sequence.add("start");
this.sequence.add("stop");
this.benzBuilder.setSequence(sequence);
return this.benzBuilder.getCarModel();
}

//B顺序的奔驰车
public CarModel getBBenzModel() {
this.sequence.clear();
this.sequence.add("engine boom");
this.sequence.add("start");
this.sequence.add("stop");
this.benzBuilder.setSequence(sequence);
return this.benzBuilder.getCarModel();
}

//C顺序的宝马车
public CarModel getCBMWModel() {
this.sequence.clear();
this.sequence.add("start");
this.sequence.add("alarm");
this.sequence.add("stop");
this.bmwBuilder.setSequence(sequence);
return this.bmwBuilder.getCarModel();
}

//D顺序的宝马车
public CarModel getDBMWModel() {
this.sequence.clear();
this.sequence.add("engine boom");
this.sequence.add("start");
this.bmwBuilder.setSequence(sequence);
return this.bmwBuilder.getCarModel();
}
}

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

director.getABenzModel().run();
director.getBBenzModel().run();
director.getCBMWModel().run();
}
}

以上是关于Builder模式的主要内容,如果未能解决你的问题,请参考以下文章

Builder设计模式,模板设计模式,Adapter设计模式笔记

builder模式-积木系列

变种 Builder 模式:优雅的对象构建方式

[05]Go设计模式:建造者模式(Builder Pattern)

设计模式Builder模式

23种设计模式之建造者模式代码实例