知识改变人生zzz.blog.chinaunix.net
zhangzhenzhong
目前主要从事C++软件开发
全部博文(60)
2013年(3)
2012年(3)
2010年(6)
2009年(23)
2008年(25)
songlw41
garyhuc
cff23674
84378567
格伯纳
Bsolar
qq9402
胡田新
屌丝变态
nhgy200
分类:
2009-04-22 02:35:15
class MotorCycleBuilder : VehicleBuilder { override public void BuilderFrame() { vehicle = new Vehicle("摩托车"); vehicle["frame"] = "MotorCycle Frame"; } public override void BuilderDoors() { vehicle["doors"] = "0"; } public override void BuilderEngine() { vehicle["engine"] = "500 cc"; } public override void BuilderWheels() { vehicle["wheels"] = "2"; } } class ScooterBuilder : VehicleBuilder { override public void BuilderFrame() { vehicle = new Vehicle("单脚滑行车"); vehicle["frame"] = "Scooter Frame"; } public override void BuilderDoors() { vehicle["doors"] = "0"; } public override void BuilderEngine() { vehicle["engine"] = "none"; } public override void BuilderWheels() { vehicle["wheels"] = "2"; } } public class Vehicle { private string type; private Hashtable parts = new Hashtable(); public Vehicle(string type) { this.type = type; } public object this[string key] { get { return parts[key]; } set { parts[key]=value; } } public void Show() { Console.WriteLine("\n----------------------------------"); Console.WriteLine("车辆类类型:"+type); Console.WriteLine("框架:"+parts["frame"]); Console.WriteLine("发动机:"+parts["engine"]); Console.WriteLine("#轮子数:"+parts["wheels"]); Console.WriteLine("#车门数:"+parts["doors"]); } } abstract class VehicleBuilder { public Vehicle vehicle; public Vehicle Vehicle { get { return vehicle; } } abstract public void BuilderEngine(); abstract public void BuilderFrame(); abstract public void BuilderWheels(); abstract public void BuilderDoors(); } class CarBuilder : VehicleBuilder { public override void BuilderFrame() { vehicle = new Vehicle("轿车"); vehicle["frame"] = "Car Frame"; } public override void BuilderDoors() { vehicle["doors"] = "4"; } public override void BuilderEngine() { vehicle["engine"] = "2500 cc"; } public override void BuilderWheels() { vehicle["wheels"] = "4"; } } class Shop { public void Construct(VehicleBuilder vehicleBuilder ) { vehicleBuilder.BuilderFrame(); vehicleBuilder.BuilderEngine(); vehicleBuilder.BuilderDoors(); vehicleBuilder.BuilderWheels(); } } class Program { 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.Vehicle.Show(); shop.Construct(b2); b2.Vehicle.Show(); shop.Construct(b3); b3.Vehicle.Show(); Console.Read(); } }
上一篇:可以用c#枚举出操作系统上所有的用户吗?
下一篇:C#创建者模式
登录 注册