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
        }
  • 相关阅读:
    android中kl布局文件加载失败解决办法
    android系统输入按键流程
    linux键值转android键值配置文件
    linux键值到Android键值的转换与自定义
    linux中ioctl的应用与说明
    zabbix邮件告警
    linux 双网关双IP设置
    随笔
    记录一次事故
    python解析.yml/.yaml文件--pyyaml模块(第三方)
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2986522.html
Copyright © 2011-2022 走看看