建造者模式:

 

定义:

将一个复杂对象的构建与它的表示分离,使得相同的构建过程能够创建不同的表示。

 

类图:

 

 

 

4个角色:

1.Product产品类
一般是实现了模板方法模式。

2.Builder抽象建造者
规范产品的组建,通常是由子类实现。

3.ConcreateBuilder
实现抽象类定义的全部方法,而且返回一个组件好的对象。

4.Director导演类
负责安排已有模块的顺序,然后告诉Builder開始建造。


 

//产品类

class Product {
	public void doSomething() {
		// 独立业务处理
	}
}

// 抽象建造者
abstract class Builder {
	// 设置产品的不同部分,以获得不同的产品
	// 不同的部分,可能是零件的不同。也可能是组装顺序的不同
	public abstract void setPart();

	// 建造产品
	public abstract Product buildProduct();
}

// 详细建造者
class ConcreateProduct extends Builder {
	private Product product = new Product();

	// 设置产品零件
	public void setPart() {
		// 产品类内的逻辑处理
	}

	// 组件一个产品
	public Product buildProduct() {
		return product;
	}
}

// 导演类
// 导演类起到封装作用,避免高层模块深入到建造者内部的实现类
class Director {
	private Builder builder = new ConcreateProduct();

	// 构建不同的产品
	public Product getAProduct() {
		builder.setPart();
		return builder.buildProduct();
	}
}


 

建造者模式最基本的功能是基本方法的调用顺序安排。也就是说说这些基本方法已经实现了。通俗地说就是零件的装配。顺序不同,产生的对象也不同。而工厂方法则重点是创建,创建零件是它的主要职责,组装顺序不是它关心的。

 

一个完整的类图:

 

 

代码:

 

abstract class CarBuilder {
	// 建造一个模型 就是组装顺序
	public abstract void setSequence(ArrayList<String> sequence);

	public abstract CarModel getCarModel();
}

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;
	}
}

class BMWBuilder extends CarBuilder {

	private BMWModel bmw = new BMWModel();

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

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

}

abstract class CarModel {
	private ArrayList<String> sequence = new 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 < sequence.size(); i++) {
			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) {
		this.sequence = sequence;
	}
}

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("奔驰---->引擎的响");
	}

}

class BMWModel 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("宝马---->引擎的响");
	}

}

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

	public BenzModel getABenzModel() {
		this.sequence.clear();
		this.sequence.add("start");
		this.sequence.add("stop");
		this.benzBuilder.setSequence(sequence);
		return (BenzModel) this.benzBuilder.getCarModel();
	}

	public BenzModel getBBenzModel() {
		this.sequence.clear();
		this.sequence.add("engine boom");
		this.sequence.add("start");
		this.benzBuilder.setSequence(sequence);
		return (BenzModel) this.benzBuilder.getCarModel();
	}

	public BMWModel getCBmwModel() {
		this.sequence.clear();
		this.sequence.add("alarm");
		this.sequence.add("start");
		this.sequence.add("stop");
		this.bmwBuilder.setSequence(sequence);
		return (BMWModel) this.bmwBuilder.getCarModel();

	}

	public BMWModel getDBmwModel() {
		this.sequence.clear();
		this.sequence.add("start");
		this.bmwBuilder.setSequence(sequence);
		return (BMWModel) this.bmwBuilder.getCarModel();

	}
}

public class Client {

	public static void main(String[] args) {
		Director director = new Director();
		System.out.println("---------------------------------------");
		for (int i = 0; i < 10; i++) {
			System.out.print( (i + 1)+"+");
			director.getABenzModel().run();

		}
		System.out.println("---------------------------------------");
		for (int i = 0; i < 5; i++) {
			System.out.print( (i + 1)+"+");
			director.getBBenzModel().run();
		}
		System.out.println("---------------------------------------");
		for (int i = 0; i < 10; i++) {
			System.out.print( (i + 1)+"+");
			director.getCBmwModel().run();
		}
		System.out.println("---------------------------------------");
		for (int i = 0; i < 5; i++) {
			System.out.print( (i + 1)+"+");
			director.getDBmwModel().run();
		}
	}
}