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

定义一个接口

 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
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值