Java 设计模式 之 建造模式

http://www.verejava.com/?id=16999036622561

package com.builder.theory;

public class TestBuilder
{
	public static void main(String[] args)
	{
		Car car=Director.construct(new ConcreteBuilder());
		System.out.println(car.getHead());
		System.out.println(car.getBody());
		System.out.println(car.getFoot());
	}
}





package com.builder.theory;

public class Car
{
	private String head;
	private String body;
	private String foot;
	
	public String getHead()
	{
		return head;
	}
	public void setHead(String head)
	{
		this.head = head;
	}
	public String getBody()
	{
		return body;
	}
	public void setBody(String body)
	{
		this.body = body;
	}
	public String getFoot()
	{
		return foot;
	}
	public void setFoot(String foot)
	{
		this.foot = foot;
	}
	
	
}





package com.builder.theory;

public class Director
{
	public static Car construct(Builder builder)
	{
		builder.buildHead();
		builder.buildBody();
		builder.buildFoot();
		return builder.buildPart();
	}
}





package com.builder.theory;

public interface Builder
{
	public void buildHead();
	public void buildBody();
	public void buildFoot();
	public Car buildPart();
}





package com.builder.theory;

public class ConcreteBuilder implements Builder
{
	Car car=null;
	
	public ConcreteBuilder()
	{
		car=new Car();
	}
	

	@Override
	public void buildHead()
	{
		car.setHead("汽车头部建造完成");
		
	}

	@Override
	public void buildBody()
	{
		car.setBody("汽车身体建造完成");
	}

	@Override
	public void buildFoot()
	{
		car.setFoot("汽车尾部建造完成");
	}

	@Override
	public Car buildPart()
	{
		return car;
	}

}


http://www.verejava.com/?id=16999036622561

posted @ 2018-06-28 09:14  verejava  阅读(100)  评论(0编辑  收藏  举报