把复杂的对象的构建与其表示分离开,以便根据程序的需要在相同的创建过程中创建不同的表示。每个生成器必须有一个相同的方法名称。
Client创建一个Director对象,指定一个build对象,配置Director。
当product需要生成时,Director通知该builder。Builder处理通知,创建product。
Client从Builder得到product。
<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 6in; HEIGHT: 242.25pt" type="#_x0000_t75"><imagedata src="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png" o:title=""></imagedata></shape>
Builder(VehicleBuilder)
指定一个接口用来创建Product对象。
ConcreteBuilder(MotoCycleBuilde,CarBuilder,ScooterBuilder)
实现Builder接口,创建product或组装product的部件。
提供返回product接口。
Director(Shop)
使用Builder接口创建对象
Product(Vehicle)
需要被创建的复杂对象。
代码:
//Director
public class Shop{
public void Construct(VechicleBuilder vehicleBuilder){
vehicleBuilder.BuildFrame();
vehicleBuilder.BuildEngine();
vehicleBuilder.BuildWheels();
vehicleBuilder.BuildDoors();
}
}
//Builder
public abstract class VehicleBuilder{
//Fields
protected Vehicle vehicle;
//Properties
public Vehicle getVehicle{
return vehicle;
}
//methods
abstract public void BuildFrame();
abstract public void BuildEngine();
abstract public void BuildWheels();
abstract public void BuildDoors();
}
//ConcreteBuilder
public class MotorCycleBuilder extends VehicleBuilder{
public void BuildFrame(){
vehicle=new Vehicle(“MotoCycle”);
vehicle.setFrame(“MotorCycle Frame”);
}
public void BuildEngine(){
vehicle.setEngine(“500 cc”);
}
public void BuildWheels(){
vehicle.setWheels(“2”);
}
public void BuildDoors(){
vehicle.setDoors(“0”);
}
}
public class CarBuilder extends VehicleBuilder{
public void BuildFrame(){
vehicle=new Vehicle(“Car”);
vehicle.setFrame(“Car Frame”);
}
public void BuildEngine(){
vehicle.setEngine(“2500 cc”);
}
public void BuildWheels(){
vehicle.setWheels(“4”);
}
public void BuildDoors(){
vehicle.setDoors(“4”);
}
}
public class ScooterBuilder extends VehicleBuilder{
public void BuildFrame(){
vehicle=new Vehicle(“Scooter”);
vehicle.setFrame(“Scooter Frame”);
}
public void BuildEngine(){
vehicle.setEngine(“none”);
}
public void BuildWheels(){
vehicle.setWheels(“2”);
}
public void BuildDoors(){
vehicle.setDoors(“0”);
}
}
//Product
public class Vechicle{
private String type;
private String frame;
private String engine;
private String wheels;
private String doors;
public Vehicle(String type){
this.type=type;
}
public void setFrame(String frame){
this.frame=frame;
}
public void setEngine (String engine){
this.engine = engine;
}
public void setWheels (String wheels){
this.wheels = wheels;
}
public void setDoors (String doors){
this.doors= doors;
}
public void Show(){
System.out.println(“----------------------------“);
System.out.println(“Vechicle Type:” +this.type);
System.out.println(“Frame:”+this.frame);
System.out.println(“Engine:”+this.engine);
System.out.println(“#Wheels:”+this.wheels);
System.out.println(“#Doors:”+this.doors);
}
public class BuilderApp{
public static void main(String[] args){
Shop shop=new Shop();
VehicleBuilder b1=new ScooterBuilder();
VehicleBuilder b2=new CarBuilder();
VehicleBuilder b3=new MotorCycleBuilder();
shop.Construct(b1);
b1.getVehicle.Show();
shop.Construct(b2);
b2.getVehicle.Show();
shop.Construct(b3);
b3.getVehicle.Show();
Output
|