建造者模式

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

建造者模式和工厂模式非常相似,它们的区别在于:建造者模式最主要功能是基本方法的调用顺序安排,也就是这些基本方法已经实现了;而工厂方法则重点是创建,你要什么对象我创造一个对象出来,组装顺序则不是他关心的。建造者模式使用的场景:产品类非常的复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建造者模式是非常合适
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值