public abstract class CarModel {
private ArrayList<String> sequence = new ArrayList<String>(); //维护一个ArrayList保存执行命令关键字
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 ++) { //根据ArrayList中保存的顺序执行相应的动作
String actionName = this.sequence.get(i);
if(actionName.equalsIgnoreCase("start")) {
this.start(); //启动汽车
} else if(actionName.equalsIgnoreCase("stop")) {
this.stop(); //停止汽车
} else if(actionName.equalsIgnoreCase("alarm")) {
this.alarm(); //汽车鸣笛
} else if(actionName.equalsIgnoreCase("engine boom")) {
this.engineBoom(); //汽车轰鸣
}
}
}
final public void setSequence(ArrayList<String> sequence) { //获得执行顺序的命令,即一个ArrayList
this.sequence = sequence;
}
}
public class BenzModel extends CarModel {
@Override
protected void start() {
System.out.println("奔驰启动……");
}
@Override
protected void stop() {
System.out.println("奔驰停止……");
}
@Override
protected void alarm() {
System.out.println("奔驰鸣笛……");
}
@Override
protected void engineBoom() {
System.out.println("奔驰轰鸣");
}
}
//宝马就略了……一样的
public class Client {
public static void main(String[] args) {
BenzModel benz = new BenzModel();
//存放run顺序
ArrayList<String> sequence = new ArrayList<String>();
sequence.add("engine boom"); //老板说:跑之前先轰鸣比较帅!
sequence.add("start");
sequence.add("stop");
//我们把这个顺序赋予奔驰
benz.setSequence(sequence);
benz.run();
}
}
public abstract class CarBuilder {
//建造一个模型,你要给我一个顺序要求
public abstract void setSequence(ArrayList<String> sequence);
//设置完毕顺序后,就可以直接拿到这个车辆模型了
public abstract CarModel getCarModel();
}
public class BenzBuilder extends CarBuilder {
private BenzModel benz = new BenzModel(); //奔驰车模型
@Override
public void setSequence(ArrayList<String> sequence) {
this.benz.setSequence(sequence); //设置奔驰车模型的运行顺序
}
@Override
public CarModel getCarModel() {
return this.benz; //将这个模型返回
}
}
//宝马车一样,不写了……
public class Client {
public static void main(String[] args) {
//存放run顺序
ArrayList<String> sequence = new ArrayList<String>();
sequence.add("engine boom");
sequence.add("start");
sequence.add("stop");
//要用这个顺序造一辆奔驰
BenzBuilder benzBuilder = new BenzBuilder();
//把顺序给奔驰组装者
benzBuilder.setSequence(sequence);
//奔驰组装者拿到顺序后就给你生产一辆来
BenzModel benz = (BenzModel) benzBuilder.getCarModel();
benz.run();
}
}
public class Director {
private ArrayList<String> sequence = new ArrayList<String>();
private BenzBuilder benzBuilder = new BenzBuilder();
private BWMBuilder bwmBuilder = new BWMBuilder();
//A顺序的奔驰车
public BenzModel getABenzModel() {
this.sequence.clear();
this.sequence.add("start");
this.sequence.add("stop");
//返回A顺序的奔驰车
this.benzBuilder.setSequence(sequence);
return (BenzModel) this.benzBuilder.getCarModel();
}
//B顺序的奔驰车
public BenzModel getBBenzModel() {
this.sequence.clear();
this.sequence.add("engine boom");
this.sequence.add("start");
this.sequence.add("stop");
//返回B顺序的奔驰车
this.benzBuilder.setSequence(sequence);
return (BenzModel) this.benzBuilder.getCarModel();
}
//C顺序的宝马车
public BenzModel getCBWMModel() {
this.sequence.clear();
this.sequence.add("start");
this.sequence.add("alarm");
this.sequence.add("stop");
//返回C顺序的宝马车
this.bwmBuilder.setSequence(sequence);
return (BenzModel) this.bwmBuilder.getCarModel();
}
//D顺序的宝马车
public BenzModel getDBWMModel() {
this.sequence.clear();
this.sequence.add("engine boom");
this.sequence.add("start");
//返回D顺序的宝马车
this.bwmBuilder.setSequence(sequence);
return (BenzModel) this.bwmBuilder.getCarModel();
}
//还有很多其他需求,设计师嘛,想啥需求就给你弄啥需求
}
public class Client {
public static void main(String[] args) {
Director director = new Director();
for(int i = 0; i < 10000; i ++) {
director.getABenzModel();
}
for(int i = 0; i < 100000; i ++) {
director.getBBenzModel();
}
for(int i = 0; i < 20000; i ++) {
director.getCBWMModel();
}
}
}