builder

package builder;

public class Plane
{
private String color;
private String shape;
private String tyre;


public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public String getShape()
{
return shape;
}
public void setShape(String shape)
{
this.shape = shape;
}
public String getTyre()
{
return tyre;
}
public void setTyre(String tyre)
{
this.tyre = tyre;
}
public String toString()
{
if(null != color && null != shape && null != tyre)
{
return "飞机的颜色是:"+color+"  形状是:"+shape+ "  轮胎是:"+tyre;
}else{
return "飞机还在研发中....";
}
}
}





package builder;
//制造飞机,就要将飞机的所有部件都建造好
public interface Builder
{
public void brush();
public void shape();
public void tyre();
public Plane getPlane();
}




package builder;

public class Worker implements Builder
{
private Plane plane;

public Worker()
{
this.plane = new Plane();
}
public void brush()
{
plane.setColor("红色");
}
public void shape()
{
plane.setShape("大鹰式");
}

public void tyre()
{
plane.setTyre("四个纳米材质的轮胎");
}

public Plane getPlane()
{
return plane;
}
}



package builder;

public class Design
{
private Builder builder;

public Design(Builder b)
{
this.builder = b;
}

public void command()
{
builder.brush();
builder.shape();
builder.tyre();
}
}



package builder;

public class Test
{
public static void main(String[] args)
{
Builder builder = new Worker();
Design design = new Design(builder);
design.command();
Plane plane = builder.getPlane();
System.out.println(plane);
}
}
/*
* 建造者模式:将一些零件组转在一起,一个builder接口,里面有产品的所有零件的制作的方法
* 和得到成品的方法。但是那些零件得要一个指挥者去指挥也就是设计者设计,根据设计一步一步
* 的完成。设计不一样制造出来的产品肯定是一样的。
*/

你可能感兴趣的:(builder)