• 利用配置文件解耦工厂类型


    定义一个接口

     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
        }
  • 相关阅读:
    java中需要注意的小细节
    利用mysql查询总数据条数,再php处理数据转出数组,生成随机返回到页面,可以做成刷新页面,出现不同的内容
    js刷新页面方法大全
    CSS3自定义滚动条样式 -webkit-scrollbar(转)
    使用jquery.qrcode生成二维码(转)
    JS鼠标事件大全 推荐收藏
    微信小程序
    js几种生成随机颜色方法
    Canvas——使用定时器模拟动态加载动画!
    H5——表单验证新特性,注册模态框!
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2986522.html
Copyright © 2020-2023  润新知