Chinaunix首页 | 论坛 | 博客
  • 博客访问: 500951
  • 博文数量: 60
  • 博客积分: 2673
  • 博客等级: 少校
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-09 00:25
个人简介

目前主要从事C++软件开发

文章分类

全部博文(60)

文章存档

2013年(3)

2012年(3)

2010年(6)

2009年(23)

2008年(25)

我的朋友

分类:

2009-04-22 02:35:15

c#实例------车间造车程序;
UML图:

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();
          
        }
    }

阅读(1926) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~