GOF《设计模式》中说道:将一个复杂对象的构建与其表示向分离,使得同样的构建过程可以创建不同的表示。
所谓“复杂对象”,是指:此对象中还含有其它的子对象。
何时采用: Builder模式所面对的情况是:各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将他们组合在 一起的算法却相对稳定。简单的说:子对象变化较频繁,组合算法相对稳定。比如生产汽车,理想状态下,生产步骤基本相同,只是不同的车款其具体部件有所不 同,变化的是部件,稳定的是生产流程,在此种情况下就采用Builder生成器模式。还比如组装电脑,组装步骤也基本就是那些步骤,但不同的使用了不同的 配件,所以也适合此模式。
因此,这是解决一个复杂对象的创建工作,现在变化的部分和相对稳定的部分已经明确,我们要做的是隔离变化,如何将子对象和算法隔离是要解决的问题。
我们现在定义一个场景:用Builder生成器生产汽车,在此处,我们有两个车款需要生产,它们是BMW或TOYOTA。
下面我们按下面步骤来实现我们的Builder生成器模式
一、抽象建造者角色:CarBuilder。它主要是用来定义两种接口,一种接口用于规范产品的各个部分的组成。比如,这里就规定了汽车五个重要组成部件的组成。第二种接口用于返回建造后的产品。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CarBuilderPattern
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 定义产品各个部件的抽象类
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 底盘抽象类
public abstract class Undercarriage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 底盘品牌
private string _undercarriagebrand;
public string MyUnderCarriageBrand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _undercarriagebrand; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _undercarriagebrand = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 底盘长度
private int _undercarriagesize;
public int MyUnderCarriageSize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _undercarriagesize; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _undercarriagesize = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 底盘构造函数
public Undercarriage()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Undercarriage(string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MyUnderCarriageBrand = BrandName;
}
#endregion
};
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 车体抽象类
public abstract class Body
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 车体品牌
private string _bodybrand;
public string BodyBrand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _bodybrand; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _bodybrand = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 车体颜色
private string _bodycolor;
public string BodyColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _bodycolor; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _bodycolor = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 车体构造函数
public Body()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Body(string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BodyBrand = BrandName;
}
#endregion
};
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 轮胎抽象类
public abstract class Wheel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string _wheelbrand;
public string WheelBrand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _wheelbrand; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _wheelbrand = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Wheel()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Wheel(string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WheelBrand = BrandName;
}
};
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 引擎抽象类
public abstract class Engine
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string _enginebrand;
public string EngineBrand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _enginebrand; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _enginebrand = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Engine()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Engine(string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EngineBrand = BrandName;
}
};
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 座位抽象类
public abstract class Seat
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string _seatbrand;
public string SeatBrand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _seatbrand; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _seatbrand = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Seat()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Seat(string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SeatBrand = BrandName;
}
};
#endregion
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 定义产品抽象类
//定义一个产品抽象类(车)
//任何具体实际产品(宝马车,RAV4,UT,BUS)要从此类继承
public abstract class Car
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 品牌属性
private string _carBrand;
public string CarBrand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _carBrand; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _carBrand = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 轮子个数属性
private int _carWheelNumber;
public int CarWheelNumber
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _carWheelNumber; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _carWheelNumber = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 座位个数属性
private int _carSeatNumber;
public int CarSeatNumber
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _carSeatNumber; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _carSeatNumber = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 车体颜色属性
private string _carBodyColor;
public string CarBodyColor
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _carBodyColor; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _carBodyColor = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 底盘长度属性
private string _carUnderCarriageSize;
public string CarUnderCarriageSize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get { return _carUnderCarriageSize; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set { _carUnderCarriageSize = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 车子构造函数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Car()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Car(string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CarBrand = BrandName;
}
#endregion
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//建造者(Builder)角色:给出一个抽象接口,以规范产品对象的各个组成部分的建造。一般而言,此接口独立于应用程序的商业逻辑。
//模式中直接创建产品对象的是具体建造者(ConcreteBuilder)角色。具体建造者类必须实现这个接口所要求的方法:一个是建造方法,另一个是结果返还方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 定义建造者角色(也是一个抽象类,无具体实现)
public abstract class Builder
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 建造方法(此产品各个组成部分的建造方法)
public abstract Undercarriage BuildUndercarriage(string brandName); //构建车底盘
public abstract Body BuildBody(string brandName); //构建车体
public abstract Wheel BuildWheel(string brandName); //构建车轮
public abstract Engine BuildEngine(string brandName); //构建发动引擎
public abstract Seat BuildSeat(string brandName); //构建车座
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 结果返回方法(返回一个此产品)
public abstract Car GetCar(Undercarriage myUndercarriage,Body myBody,Engine myEngine,Seat[] mySeats,Wheel[] myWheel);
#endregion
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二、建造者模式的指导者:CarFactoryManager。指导者做的是稳定的建造工作,他按照固定的流程,重复做着相同的装配工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CarBuilderPattern
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//此部分是最稳定的,不会因为车款的变化而变化
//因为此处定义了一个造车的流程,不管造什么车,基本流程大致相同
class CarFactoryManager
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//具体建造者(Concrete Builder)角色:担任这个角色的是于应用程序紧密相关的类,它们在应用程序调用下创建产品实例。
//这个角色主要完成的任务包括:实现Builder角色提供的接口,一步一步完成创建产品实例的过程;在建造过程完成后,提供产品的实例。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Car CreateCars(Builder builder,string BrandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 一步一步完成创建产品实例的过程
//定义了一个造车的流程(此流程可以根据xml或数据库获取如何造车的要求并在此处根据要求来造车)
//此处,我们假设根据外部要求,我们在此处要造的车为:一个底盘,四个座位,一个车体,四个门,四个轮子,一个引擎
Undercarriage myundercarriage= builder.BuildUndercarriage(BrandName); //安装底盘
Body mybody= builder.BuildBody(BrandName); //安装车体
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Engine myengine= builder.BuildEngine(BrandName); //安装引擎
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//安装四个座位
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Seat[] seats = {
builder.BuildSeat(BrandName),
builder.BuildSeat(BrandName),
builder.BuildSeat(BrandName),
builder.BuildSeat(BrandName)
};
//安装四个轮胎
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Wheel[] wheels ={
builder.BuildWheel(BrandName),
builder.BuildWheel(BrandName),
builder.BuildWheel(BrandName),
builder.BuildWheel(BrandName)
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 提供产品的实例
return builder.GetCar(myundercarriage,mybody,myengine,seats,wheels);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三、具体的建造者:BMWCar和TOYOTACar。他的工作就是实现各建造步骤的接口,以及实现返回产品的接口。
宝马车建造者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CarBuilderPattern
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//此部分是经常变化的
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 宝马车各部件的具体定义类:继承各部件的抽象类
//我们可以在此处定义各部件的具体特性,如:不同的颜色,不同的尺寸,不同的外形等
public class BMWUndercarriage:Undercarriage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class BMWBody :Body
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class BMWWheel:Wheel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class BMWEngine :Engine
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class BMWSeat:Seat
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 定义一个具体产品类:宝马车,它继承自前面定义的抽象类Car
public class BMWCar:Car
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 具体实现宝马车的Builder
public class BMWBuilder : Builder
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//定义宝马车具体如何构建各个部件的方法
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 宝马车构建车底盘方法
public override Undercarriage BuildUndercarriage(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BMWUndercarriage undercarriageBMW = new BMWUndercarriage();
undercarriageBMW.MyUnderCarriageBrand = brandName;
undercarriageBMW.MyUnderCarriageSize = 4;
return undercarriageBMW;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 宝马车构建车体
public override Body BuildBody(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BMWBody bodyBMW = new BMWBody();
bodyBMW.BodyBrand = brandName;
bodyBMW.BodyColor = "红色";
return bodyBMW;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 宝马车构建车轮
public override Wheel BuildWheel(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BMWWheel wheelBMW = new BMWWheel();
wheelBMW.WheelBrand = brandName;
return wheelBMW;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 宝马车构建发动引擎
public override Engine BuildEngine(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BMWEngine EngineBMW= new BMWEngine();
EngineBMW.EngineBrand = brandName;
return EngineBMW;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 宝马车构建车座
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override Seat BuildSeat(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BMWSeat SeatBMW = new BMWSeat();
SeatBMW.SeatBrand = brandName;
return SeatBMW;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 返回产品
public override Car GetCar(Undercarriage myUndercarriage, Body myBody, Engine myEngine, Seat[] mySeats, Wheel[] myWheel)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BMWCar mycar = new BMWCar();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//在这里对各个部件进行组合和处理
mycar.CarBrand = "宝马车";
Console.WriteLine("本次Builder工厂制造的是"+mycar.CarBrand.ToString());
Console.WriteLine("底盘用的是:" +myUndercarriage.MyUnderCarriageBrand.ToString());
Console.WriteLine("底盘长度是:" + myUndercarriage.MyUnderCarriageSize.ToString()+"米");
Console.WriteLine("车体用的是:"+myBody.BodyBrand.ToString());
Console.WriteLine("车体用颜色是:" + myBody.BodyColor.ToString());
Console.WriteLine("引擎用的是:" + myEngine.EngineBrand.ToString()+"引擎");
Console.WriteLine("座位有"+mySeats.Length.ToString()+"个");
Console.WriteLine("轮胎有" + myWheel.Length.ToString() + "个");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 返回成型产品
return mycar;
#endregion
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
丰田车建造者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CarBuilderPattern
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 丰田车各部件的具体定义类:继承各部件的抽象类
//我们可以在此处定义各部件的具体特性,如:不同的颜色,不同的尺寸,不同的外形等
public class TOYOTAUndercarriage : Undercarriage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class TOYOTABody : Body
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class TOYOTAWheel : Wheel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class TOYOTAEngine : Engine
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
public class TOYOTASeat : Seat
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ };
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 定义一个具体产品类:丰田车,它继承自前面定义的抽象类Car
public class TOYOTACar : Car
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 具体实现丰田车的Builder
public class TOYOTABuilder : Builder
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//定义丰田车具体如何构建各个部件的方法
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 丰田车构建车底盘方法
public override Undercarriage BuildUndercarriage(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TOYOTAUndercarriage undercarriageTOYOTA = new TOYOTAUndercarriage();
undercarriageTOYOTA.MyUnderCarriageBrand = brandName;
undercarriageTOYOTA.MyUnderCarriageSize = 3;
return undercarriageTOYOTA;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 丰田车构建车体
public override Body BuildBody(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TOYOTABody bodyTOYOTA = new TOYOTABody();
bodyTOYOTA.BodyBrand = brandName;
bodyTOYOTA.BodyColor = "兰色";
return bodyTOYOTA;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 丰田车构建车轮
public override Wheel BuildWheel(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TOYOTAWheel wheelTOYOTA = new TOYOTAWheel();
wheelTOYOTA.WheelBrand = brandName;
return wheelTOYOTA;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 丰田车构建发动引擎
public override Engine BuildEngine(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TOYOTAEngine EngineTOYOTA = new TOYOTAEngine();
EngineTOYOTA.EngineBrand = brandName;
return EngineTOYOTA;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 丰田车构建车座
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override Seat BuildSeat(string brandName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TOYOTASeat SeatTOYOTA = new TOYOTASeat();
SeatTOYOTA.SeatBrand = brandName;
return SeatTOYOTA;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
#region 对丰田车的上述各部件进行具体组合,形成产品然后返回
public override Car GetCar(Undercarriage myUndercarriage, Body myBody, Engine myEngine, Seat[] mySeats, Wheel[] myWheel)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TOYOTACar mycar = new TOYOTACar();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//在这里对各个部件进行组合和处理
mycar.CarBrand = "丰田车";
Console.WriteLine("本次Builder工厂制造的是" + mycar.CarBrand.ToString());
Console.WriteLine("底盘用的是:" + myUndercarriage.MyUnderCarriageBrand.ToString());
Console.WriteLine("底盘长度是:" + myUndercarriage.MyUnderCarriageSize.ToString() + "米");
Console.WriteLine("车体用的是:" + myBody.BodyBrand.ToString());
Console.WriteLine("车体用颜色是:" + myBody.BodyColor.ToString());
Console.WriteLine("引擎用的是:" + myEngine.EngineBrand.ToString() + "引擎");
Console.WriteLine("座位有" + mySeats.Length.ToString() + "个");
Console.WriteLine("轮胎有" + myWheel.Length.ToString() + "个");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
返回成型产品 返回成型产品
return mycar;
#endregion
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
四、 Car就是建造出来的复杂产品--汽车。在代码中,我们的各种建造步骤都是为创建产品中的各种配件服务的,Car定义了一个相对具体的产品,在应用中可以把这个产品进行比较高度的抽象,使得不同的具体建造者甚至可以建造出完全不同的产品。
五、 看看客户端的代码,用户先是选择了一个具体的Builder,BMWCar或TOYOTACar,然后交由Builder工厂给他生产出相应的汽车产品。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CarBuilderPattern
{
class Program
{
static void Main(string[] args)
{
//指导者(Director)角色:担任这个角色的类调用具体建造者角色(BMWBuilder)以创建产品对象。
//导演者并没有产品类的具体知识,真正拥有产品类的具体知识的是具体建造者对象。
Car mycaBMW= CarFactoryManager.CreateCars(new BMWBuilder(),"宝马车");
Car mycarToyota = CarFactoryManager.CreateCars(new TOYOTABuilder(), "丰田车");
Console.Read();
}
}
}
前往:设计模式学习笔记清单