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

工厂模式

时间:2015-05-26 15:47:14      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 工厂模式
{
    class Program
    {
        public interface IFruit
        {
        }
        public class Orange : IFruit
        {
            public Orange()
            {
                Console.WriteLine("An orange is got!");
            }
        }
        public class Apple : IFruit
        {
            public Apple()
            {
                Console.WriteLine("An apple is got!");
            }
        }

        public class FruitFactory
        {
            public Orange MakeOrange()
            {
                return new Orange();
            }
            public Apple MakeApple()
            {
                return new Apple();
            }
        }

        static void Main(string[] args)
        {
            string FruitName = Console.ReadLine();
            IFruit MyFruit = null;
            FruitFactory MyFruitFactory = new FruitFactory();
            switch (FruitName)
            {
                case "Orange":
                    MyFruit = MyFruitFactory.MakeOrange();
                    break;
                case "Apple":
                    MyFruit = MyFruitFactory.MakeApple();
                    break;
                default:
                    break;
            }
            Console.ReadLine();
        }
    }
}

 

工厂模式

标签:

原文地址:http://www.cnblogs.com/fanxingthink/p/4530515.html

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