zoukankan      html  css  js  c++  java
  • 利用配置文件解耦工厂类型

    定义一个接口

     public interface IFactory
        {
            IProduct Create();              //  返回默认的 concrete product
            IProduct Create(string name);   //  按照配置的逻辑名称返回指定的concrete product
        }

    获取配置类

     public class ProductRegistry
        {
            const string SectionName = "concreteProducts";
            static NameValueCollection collection =
                (NameValueCollection)ConfigurationManager.GetSection(SectionName);
    
            public Type this[string name]
            {
                get { return Type.GetType(collection[name]); }
            }
        }

    工厂

     public class Factory : IFactory
        {
    
            public const string DefaultName = "DEFAULT";
            public readonly ProductRegistry productRegistry = new ProductRegistry();
    
            #region IFactory Members
    
            public IProduct Create()
            {
                return (IProduct)Activator.CreateInstance(productRegistry[DefaultName]);
            }
    
            public IProduct Create(string name)
            {
                if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
                return (IProduct)Activator.CreateInstance(productRegistry[name]);
            }
    
            #endregion
        }

    page 84,只为自己阅读,复习

    如果批量生产的话

      public interface IBatchFactory : IFactory
        {
            IEnumerable<IProduct> Create(string name, int quantity);
            IEnumerable<IProduct> Create(int quantity);        
        }
     public class BatchFactory : Factory, IBatchFactory
        {
            #region IBatchFactory Members
    
            /// <summary>
            /// batch create
            /// </summary>
            /// <param name="quantity"></param>
            /// <returns></returns>
            public IEnumerable<IProduct> Create(int quantity)
            {
                if (quantity <= 0) throw new IndexOutOfRangeException("quantity");
                return InternalCreate<IProduct>(productRegistry[DefaultName], quantity);
            }
    
            /// <summary>
            /// batch create
            /// </summary>
            /// <param name="name"></param>
            /// <param name="quantity"></param>
            /// <returns></returns>
            public IEnumerable<IProduct> Create(string name, int quantity)
            {
                if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
                return InternalCreate<IProduct>(productRegistry[name], quantity);
            }
    
            /// <summary>
            /// Helper method
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="type"></param>
            /// <param name="quantity"></param>
            /// <returns></returns>
            IEnumerable<T> InternalCreate<T>(Type type, int quantity)
            {
                if (quantity <= 0) throw new IndexOutOfRangeException("quantity");
                if (type == null) throw new ArgumentNullException("type");
                IList<T> result = new List<T>();
                for (int i = 0; i < quantity; i++)
                    result.Add((T)Activator.CreateInstance(type));
                return result;
            }
    
            #endregion
        }
  • 相关阅读:
    开发中的一些总结2
    XML与DataTable/DataSet互转(C#) 把数据库中表的内容转存为XML文件
    闲来无事。。。。
    一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码:
    20120301 14:10 js函数内部实现休眠
    设为首页和收藏本站的代码
    Jquery实现对a标签改变选中的背景色 支持多选 再次点击背景色消失
    asp.net上传图片并生成等比例缩略图(.Net自带函数完成)
    类中只有 成员变量 和 一个成员函数表
    CListCtrl 使用技巧
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2986522.html
Copyright © 2011-2022 走看看