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

抽象工厂

时间:2016-05-28 01:07:37      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

意图

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

实现

工厂类

    /// <summary>
    /// 美食工厂
    /// </summary>
    public abstract class CateFactory
    {
        public abstract HuoGuo GetHuoGuo();
        public abstract XiaoChi GetXiaoChi();
    }
    public class ChongQingCateFactory : CateFactory
    {
        public override HuoGuo GetHuoGuo()
        {
            return new ChongQingHuoGuo();
        }

        public override XiaoChi GetXiaoChi()
        {
            return new ChongQingXiaoChi();
        }
    }
    public class NanChangCateFactory : CateFactory
    {
        public override HuoGuo GetHuoGuo()
        {
            return new NanChangHuoGuo();
        }

        public override XiaoChi GetXiaoChi()
        {
            return new NanChangXiaoChi();
        }
    }

美食-火锅

    /// <summary>
    /// 火锅
    /// </summary>
    public abstract class HuoGuo
    {
        public abstract void show();
    }
    public class ChongQingHuoGuo : HuoGuo
    {
        public override void show()
        {
            Console.WriteLine("重庆火锅");
        }
    }
    public class NanChangHuoGuo : HuoGuo
    {
        public override void show()
        {
            Console.WriteLine("南昌火锅");
        }
    }

美食-小吃

    /// <summary>
    /// 小吃
    /// </summary>
    public abstract class XiaoChi
    {
        public abstract void Show();
    }
    public class ChongQingXiaoChi : XiaoChi
    {
        public override void Show()
        {
            Console.WriteLine("重庆小吃");
        }
    }
    public class NanChangXiaoChi : XiaoChi
    {
        public override void Show()
        {
            Console.WriteLine("南昌小吃");
        }
    }

客户端

    class Program
    {
        static void Main(string[] args)
        {
            CateFactory factory = new ChongQingCateFactory();
            factory.GetHuoGuo().show();
            factory.GetXiaoChi().Show();
            Console.ReadLine();
        }
    }

 

抽象工厂

标签:

原文地址:http://www.cnblogs.com/Jabben/p/5536507.html

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