//Builderpattern--StructuralexampleusingSystem;usingSystem.Collections;//"Director"classDirector{//MethodspublicvoidConstruct(Builderbuilder){builder.BuildPartA();builder.BuildPartB();}}//"Builder"abstractclassBuilder{//MethodsabstractpublicvoidBuildPartA();abstractpublicvoidBuildPartB();abstractpublicProductGetResult();}//"ConcreteBuilder1"classConcreteBuilder1:Builder{//FieldsprivateProductproduct;//MethodsoverridepublicvoidBuildPartA(){product=newProduct();product.Add("PartA");}overridepublicvoidBuildPartB(){product.Add("PartB");}overridepublicProductGetResult(){returnproduct;}}//"ConcreteBuilder2"classConcreteBuilder2:Builder{//FieldsprivateProductproduct;//MethodsoverridepublicvoidBuildPartA(){product=newProduct();product.Add("PartX");}overridepublicvoidBuildPartB(){product.Add("PartY");}overridepublicProductGetResult(){returnproduct;}}//"Product"classProduct{//FieldsArrayListparts=newArrayList();//MethodspublicvoidAdd(stringpart){parts.Add(part);}publicvoidShow(){Console.WriteLine("ProductParts-------");foreach(stringpartinparts)Console.WriteLine(part);}}///
///Clienttest///
publicclassClient{publicstaticvoidMain(string[]args){//CreatedirectorandbuildersDirectordirector=newDirector();Builderb1=newConcreteBuilder1();Builderb2=newConcreteBuilder2();//Constructtwoproductsdirector.Construct(b1);Productp1=b1.GetResult();p1.Show();director.Construct(b2);Productp2=b2.GetResult();p2.Show();}}客户端负责创建指导者和具体建造者对象。然后,客户把具体建造者对象交给指导者。客户一声令下,指导者操纵建造者开始创建产品。当产品创建完成后,建造者把产品返还给客户端。下面的程序代码演示了Shop对象使用VehicleBuilders来建造不同的交通工具。该例子使用了Builder模式顺序建造交通工具的不同部分。//Builderpattern--RealWorldexampleusingSystem;usingSystem.Collections;//"Director"classShop{//MethodspublicvoidConstruct(VehicleBuildervehicleBuilder){vehicleBuilder.BuildFrame();vehicleBuilder.BuildEngine();vehicleBuilder.BuildWheels();vehicleBuilder.BuildDoors();}}//"Builder"abstractclassVehicleBuilder{//FieldsprotectedVehiclevehicle;//PropertiespublicVehicleVehicle{get{returnvehicle;}}//MethodsabstractpublicvoidBuildFrame();abstractpublicvoidBuildEngine();abstractpublicvoidBuildWheels();abstractpublicvoidBuildDoors();}//"ConcreteBuilder1"classMotorCycleBuilder:VehicleBuilder{//MethodsoverridepublicvoidBuildFrame(){vehicle=newVehicle("MotorCycle");vehicle["frame"]="MotorCycleFrame";}overridepublicvoidBuildEngine(){vehicle["engine"]="500cc";}overridepublicvoidBuildWheels(){vehicle["wheels"]="2";}overridepublicvoidBuildDoors(){vehicle["doors"]="0";}}//"ConcreteBuilder2"classCarBuilder:VehicleBuilder{//MethodsoverridepublicvoidBuildFrame(){vehicle=newVehicle("Car");vehicle["frame"]="CarFrame";}overridepublicvoidBuildEngine(){vehicle["engine"]="2500cc";}overridepublicvoidBuildWheels(){vehicle["wheels"]="4";}overridepublicvoidBuildDoors(){vehicle["doors"]="4";}}//"ConcreteBuilder3"classScooterBuilder:VehicleBuilder{//MethodsoverridepublicvoidBuildFrame(){vehicle=newVehicle("Scooter");vehicle["frame"]="ScooterFrame";}overridepublicvoidBuildEngine(){vehicle["engine"]="none";}overridepublicvoidBuildWheels(){vehicle["wheels"]="2";}overridepublicvoidBuildDoors(){vehicle["doors"]="0";}}//"Product"classVehicle{//Fieldsprivatestringtype;privateHashtableparts=newHashtable();//ConstructorspublicVehicle(stringtype){this.type=type;}//Indexerspublicobjectthis[stringkey]{get{returnparts[key];}set{parts[key]=value;}}//MethodspublicvoidShow(){Console.WriteLine("---------------------------");Console.WriteLine("VehicleType:"+type);Console.WriteLine("Frame:"+parts["frame"]);Console.WriteLine("Engine:"+parts["engine"]);Console.WriteLine("#Wheels:"+parts["wheels"]);Console.WriteLine("#Doors:"+parts["doors"]);}}///
///BuilderApptest///
publicclassBuilderApp{publicstaticvoidMain(string[]args){//CreateshopandvehiclebuildersShopshop=newShop();VehicleBuilderb1=newScooterBuilder();VehicleBuilderb2=newCarBuilder();VehicleBuilderb3=newMotorCycleBuilder();//Constructanddisplayvehiclesshop.Construct(b1);b1.Vehicle.Show();shop.Construct(b2);b2.Vehicle.Show();shop.Construct(b3);b3.Vehicle.Show();}}建造者模式在使用的过程中可以演化出多种形式。省略抽象建造者角色如果系统中只需要一个具体建造者的话,可以省略掉抽象建造者。这时代码可能如下://"Director"classDirector{privateConcreteBuilderbuilder;//MethodspublicvoidConstruct(){builder.BuildPartA();builder.BuildPartB();}}省略指导者角色在具体建造者只有一个的情况下,如果抽象建造者角色已经被省略掉,那么还可以省略掉指导者角色。