package com.test.design.builder;
import java.util.ArrayList;
public abstract class CarModel {
private ArrayList<String> sequence = new ArrayList<String>();
//protected 对外不用暴露那么多方法
protected abstract void start();
protected abstract void stop();
protected abstract void engineBoom();
protected abstract void alarm();
public final void run(){
int size = this.sequence.size();
for(int i = 0; i < size; i++){
if(this.sequence.get(i).equals("start")){
this.start();
}else if(this.sequence.get(i).equals("stop")){
this.stop();
}else if(this.sequence.get(i).equals("engineBoom")){
this.engineBoom();
}else if(this.sequence.get(i).equals("alarm")){
this.alarm();
}
}
}
//设置方法调用顺序
public final void setSequence(ArrayList<String> sequence){
this.sequence = sequence;
}
}
package com.test.design.builder;
public class BigCar extends CarModel {
@Override
protected void alarm() {
System.out.println("大车喇叭响了...");
}
@Override
protected void engineBoom() {
System.out.println("大车引擎启动了...");
}
@Override
protected void start() {
System.out.println("大车动起来了...");
}
@Override
protected void stop() {
System.out.println("大车停下来了...");
}
}
package com.test.design.builder;
public class SmallCar extends CarModel {
@Override
protected void alarm() {
System.out.println("小车喇叭响了...");
}
@Override
protected void engineBoom() {
System.out.println("小车引擎启动了...");
}
@Override
protected void start() {
System.out.println("小车动起来了...");
}
@Override
protected void stop() {
System.out.println("小车停下来了...");
}
}
package com.test.design.builder;
import java.util.ArrayList;
public abstract class CarBuilder {
public abstract CarModel getCarModel();
public abstract void setSequence(ArrayList<String> sequence);
}
package com.test.design.builder;
import java.util.ArrayList;
public class BigCarBuilder extends CarBuilder {
private BigCar bigCar = new BigCar();
@Override
public CarModel getCarModel() {
return this.bigCar;
}
@Override
public void setSequence(ArrayList<String> sequence) {
this.bigCar.setSequence(sequence);
}
}
package com.test.design.builder;
import java.util.ArrayList;
public class SmallCarBuilder extends CarBuilder {
private SmallCar smallCar = new SmallCar();
@Override
public CarModel getCarModel() {
return this.smallCar;
}
@Override
public void setSequence(ArrayList<String> sequence) {
this.smallCar.setSequence(sequence);
}
}
package com.test.design.builder;
import java.util.ArrayList;
public class Director {
private ArrayList<String> sequence = new ArrayList<String>();
private BigCarBuilder bigCarBuilder = new BigCarBuilder();
private SmallCarBuilder smallCarBuilder = new SmallCarBuilder();
public BigCar getABigCar(){
this.sequence.clear();
this.sequence.add("start");
this.sequence.add("alarm");
this.sequence.add("engineBoom");
this.sequence.add("stop");
this.bigCarBuilder.setSequence(this.sequence);
return (BigCar)this.bigCarBuilder.getCarModel();
}
public BigCar getBBigCar(){
this.sequence.clear();
this.sequence.add("alarm");
this.sequence.add("engineBoom");
this.sequence.add("start");
this.sequence.add("stop");
this.bigCarBuilder.setSequence(this.sequence);
return (BigCar)this.bigCarBuilder.getCarModel();
}
public SmallCar getASmallCar(){
this.sequence.clear();
this.sequence.add("start");
this.sequence.add("alarm");
this.sequence.add("engineBoom");
this.sequence.add("stop");
this.smallCarBuilder.setSequence(this.sequence);
return (SmallCar)this.smallCarBuilder.getCarModel();
}
public SmallCar getBSmallCar(){
this.sequence.clear();
this.sequence.add("alarm");
this.sequence.add("engineBoom");
this.sequence.add("start");
this.sequence.add("stop");
this.smallCarBuilder.setSequence(this.sequence);
return (SmallCar)this.smallCarBuilder.getCarModel();
}
}
package com.test.design.builder;
public class Client {
public static void main(String[] args) {
Director director = new Director();
director.getABigCar().run();
director.getBBigCar().run();
director.getASmallCar().run();
director.getBSmallCar().run();
}
}
建造者模式和工厂模式非常相似,它们的区别在于:建造者模式最主要功能是基本方法的调用顺序安排,也就是这些基本方法已经实现了;而工厂方法则重点是创建,你要什么对象我创造一个对象出来,组装顺序则不是他关心的。建造者模式使用的场景:产品类非常的复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建造者模式是非常合适