Java设计模式三

建造者模式

当我们思考通过复杂的零件来生成一个完整的产品时,就用到了今天要说的主题-建造者模式,下面我们实际的代码来分析建造者模式的设计
假设飞机起飞需要有多个步骤,但是每种型号的飞机起飞的步骤又不相同,这时我们想要建造一个可以起飞多种型号飞机的工程。
1.首先我们先预先定义一个抽象的飞机起飞类,此类中包含启动发动机、轮滑、推力式发射、弹射、起飞等操作。


public abstract class Airplane {
	//定义每个步骤的执行顺序
	private ArrayList<String> sequence = new ArrayList<>();
	//飞机打开发动机 
	protected abstract void start();
	//飞机滑行
	protected abstract void glide();
	//飞机被推力发射
	protected abstract void emit();
	//飞机弹射
	protected abstract void popup();
	//开始起飞
	protected abstract void hopoff();
	//设定传递方法
	final public void setSequence(ArrayList<String> sequence) {
		this.sequence = sequence;
	}
	//飞机起飞步骤
	final public void run() {
		for(int i = 0;i<this.sequence.size();i++) {
			String actionName = this.sequence.get(i);
			if(actionName.equalsIgnoreCase("start")) {
				this.start();
			}else if(actionName.equalsIgnoreCase("glide")) {
				this.glide();
			}else if(actionName.equalsIgnoreCase("emit")) {
				this.emit();
			}else if(actionName.equalsIgnoreCase("popup")) {
				this.popup();
			}else if(actionName.equalsIgnoreCase("hopoff")) {
				this.hopoff();
			}
		}
	}
	
}

class J10 extends Airplane{

	@Override
	protected void start() {
		// TODO Auto-generated method stub
		System.out.println("J10启动发动机");
	}

	@Override
	protected void glide() {
		// TODO Auto-generated method stub
		System.out.println("J10开始滑行");

	}

	@Override
	protected void emit() {
		// TODO Auto-generated method stub
		System.out.println("J10被发射");
	}

	@Override
	protected void popup() {
		// TODO Auto-generated method stub
		System.out.println("J10被弹射");
	}

	@Override
	protected void hopoff() {
		// TODO Auto-generated method stub
		System.out.println("J10成功起飞");
	}
	
}

class J15 extends Airplane{
	@Override
	protected void start() {
		// TODO Auto-generated method stub
		System.out.println("J15启动发动机");
	}

	@Override
	protected void glide() {
		// TODO Auto-generated method stub
		System.out.println("J15开始滑行");

	}

	@Override
	protected void emit() {
		// TODO Auto-generated method stub
		System.out.println("J15被发射");
	}

	@Override
	protected void popup() {
		// TODO Auto-generated method stub
		System.out.println("J15被弹射");
	}

	@Override
	protected void hopoff() {
		// TODO Auto-generated method stub
		System.out.println("J15成功起飞");
	}
}

2.定义建造类-定义每种类型飞机的起飞步骤


/**
 * 飞机组装类
 *
 */
public abstract class AirBuilder {

	public abstract void setSequence(ArrayList<String> sequence);
	//设置飞机的不同型号
	public abstract Airplane getAirplane();
}

class J10Builder extends AirBuilder{
	private J10 j10 = new J10();

	@Override
	public void setSequence(ArrayList<String> sequence) {
		// TODO Auto-generated method stub
		this.j10.setSequence(sequence);
	}

	@Override
	public Airplane getAirplane() {
		// TODO Auto-generated method stub
		return this.j10;
	}
	
}

class J15Builder extends AirBuilder{
	private J15 j15 = new J15();

	@Override
	public void setSequence(ArrayList<String> sequence) {
		// TODO Auto-generated method stub
		this.j15.setSequence(sequence);
	}

	@Override
	public Airplane getAirplane() {
		// TODO Auto-generated method stub
		return this.j15;
	}
	
}

3.开始起飞测试


/**
 * 飞行测试
 *
 */
public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       ArrayList<String> sequence1 = new ArrayList<>();
       sequence1.add("start");//设置J10开始发动
       sequence1.add("glide");//开始滑行
       sequence1.add("hopoff");//成功起飞
       ArrayList<String> sequence2 = new ArrayList<>();
       sequence2.add("start");//设置J150开始发动
       sequence2.add("emit");//开始弹射
       sequence2.add("hopoff");//成功起飞
       //实例化一架J10
       J10Builder j10builder = new J10Builder();
       j10builder.setSequence(sequence1);
       J10 j10 = (J10) j10builder.getAirplane();
       j10.run();
       System.out.println();
       J15Builder j15Builder = new J15Builder();
       j15Builder.setSequence(sequence2);
       J15 j15 = (J15) j15Builder.getAirplane();
       j15.run();
	}

}

运行结果:

批量建造

/**
 * 飞机场-定义每种型号飞机的具体起飞方法
 */
public class AirPort {
	private ArrayList<String> sequence = new ArrayList<>();//定义起飞步骤顺序
	private J10Builder j10builder = new J10Builder();
	private J15Builder j15builder = new J15Builder();
	/**
	 * 定义J10起飞步骤
	 * @return J10
	 */
	public J10 getJ10model() {
	    this.sequence.clear();
	    sequence.add("start");//设置J10开始发动
	    sequence.add("glide");//开始滑行
	    sequence.add("hopoff");//成功起飞
	    
	    this.j10builder.setSequence(sequence);
	    return (J10)this.j10builder.getAirplane();
	}
	/**
	 * 定义J150起飞步骤
	 * @return J15
	 */
	public J15 getJ15model() {
	    this.sequence.clear();
	    sequence.add("start");//设置J15开始发动
	    sequence.add("emit");//弹射
	    sequence.add("hopoff");//成功起飞
	    
	    this.j15builder.setSequence(sequence);
	    return (J15)this.j15builder.getAirplane();
	}

}

定义测试类


public class NewClient {
	public static void main(String[] args) {
		AirPort airport = new AirPort();
		for (int i = 0; i<=20; i++) {
			System.out.println("第"+i+"架J10开始起飞");
			airport.getJ10model().run();
		}
		for (int i = 0; i<=30; i++) {
			System.out.println("第"+i+"架J15开始起飞");
			airport.getJ15model().run();
		}
	}
	
}

运行结果:

以上代码可以细细品味这样设计的好处,建造者模式便于我们控制细节,并且使得程序容易扩展。