zoukankan      html  css  js  c++  java
  • 工厂模式的传统实现

    using System;
    using System.Collections.Generic;
    using System.Text;
    // Download by http://www.codefans.net
    namespace NET.MST.Sixth.TraFactory
    {
        /// <summary>
        /// 使用者
        /// </summary>
        class Customer
        {
            static void Main(string[] args)
            {
                //根据需要获得不同的产品零件
                IProduct window = FactoryManager.GetProduct(RoomParts.Window);
                IProduct roof = FactoryManager.GetProduct(RoomParts.Roof);
                IProduct pillar = FactoryManager.GetProduct(RoomParts.Pillar);
            }
        }
        /// <summary>
        /// 屋子产品的零件
        /// </summary>
        enum RoomParts
        {
            Roof,
            Window,
            Pillar
        }
        /// <summary>
        /// 工厂接口
        /// </summary>
        interface IFactory
        {
            IProduct Produce();
        }
        /// <summary>
        /// 产品接口
        /// </summary>
        interface IProduct
        {
            String GetName();
        }
        /// <summary>
        /// 工厂管理者
        /// </summary>
        class FactoryManager
        {
            public static IProduct
                GetProduct(RoomParts part)
            {
                IFactory factory=null;
                //这里就是传统工厂模式的弊端
                //工厂管理者和工厂类族耦合
                switch (part)
                {
                    case RoomParts.Pillar:
                        factory = new PillarFactory();
                        break;
                    case RoomParts.Roof:
                        factory = new RoofFactory();
                        break;
                    case RoomParts.Window:
                        factory = new WindowFactory();
                        break;
                }
                IProduct product=factory.Produce();
                Console.WriteLine("生产了一个产品:" +
                        product.GetName());
                return product;
            }
        }
        #region 工厂类族
        class RoofFactory : IFactory
        {
            public IProduct Produce()
            {
                return new Roof();
            }
        }
        class WindowFactory : IFactory
        {
            public IProduct Produce()
            {
                return new Window();
            }
        }
        class PillarFactory : IFactory
        {
            public IProduct Produce()
            {
                return new Pillar();
            }
        }
        #endregion

        #region 产品类族
        class Roof : IProduct
        {
            public String GetName()
            {
                return "屋顶";
            }
        }
        class Window : IProduct
        {
            public String GetName()
            {
                return "窗户";
            }
        }
        class Pillar : IProduct
        {
            public String GetName()
            {
                return "柱子";
            }
        }
        #endregion
    }

  • 相关阅读:
    常量池 栈 堆
    函数的调用
    字符、数组
    整理
    JavaScript深入之——原型与原型链
    虚拟机(VMware)中文破解版,及创建虚拟机
    小程序之mpvue使用
    报错整理及解决办法
    js 关于时间方面的通用函数
    iview Cascader级联选择省市区问题
  • 原文地址:https://www.cnblogs.com/jasonjiang/p/1763557.html
Copyright © 2011-2022 走看看