生成器模式(Builder)

博客介绍了建造者模式,将复杂对象的构建与表示分离,可在相同创建过程中创建不同表示。阐述了Client、Director、Builder和Product的交互流程,还给出了建造者模式的代码示例,包括Director、Builder、ConcreteBuilder和Product的具体实现。

把复杂的对象的构建与其表示分离开,以便根据程序的需要在相同的创建过程中创建不同的表示。每个生成器必须有一个相同的方法名称。

Client创建一个Director对象,指定一个build对象,配置Director

product需要生成时,Director通知该builderBuilder处理通知,创建product

ClientBuilder得到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>

BuilderVehicleBuilder

指定一个接口用来创建Product对象。

ConcreteBuilderMotoCycleBuildeCarBuilderScooterBuilder

实现Builder接口,创建product或组装product的部件。

提供返回product接口。

DirectorShop

使用Builder接口创建对象

ProductVehicle

需要被创建的复杂对象。

代码:

//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

---------------------------
Vehicle Type: Scooter
Frame : Scooter Frame
Engine : none
#Wheels: 2
#Doors : 0

---------------------------
Vehicle Type: Car
Frame : Car Frame
Engine : 2500 cc
#Wheels: 4
#Doors : 4

---------------------------
Vehicle Type: MotorCycle
Frame : MotorCycle Frame
Engine : 500 cc
#Wheels: 2
#Doors : 0

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值