码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂模式

时间:2015-08-28 19:02:17      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

从网上查了一些资料,自己也总结一下

 


技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace factory
{

    //抽象产品角色

    public interface Car
    {

        void Drive();

    }
    //具体产品角色1
    public class BaoMa : Car
    {
        public void Drive()
        {
            Console.WriteLine("baoma");
        }
    }
    //具体产品角色2
    public class BenChi : Car
    {
        public void Drive()
        {
            Console.WriteLine("benchi");
        }
    }
    //工厂类角色
    public class Driver
    {

        public void DriveCar(string car)
        {
            if (car == "baoma")
            {
                new BaoMa().Drive();
            }
            else
            {
                new BenChi().Drive();
            }
        }


    }
    class Program
    {
        static void Main(string[] args)
        {
           new Driver().DriveCar("benchi");

           new Driver().DriveCar("baoma");
           Console.ReadKey();
        }
    }
}

简单工厂模式

标签:

原文地址:http://www.cnblogs.com/xuguanghui/p/4766995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!