c典型工厂化实现实例C工厂模式之精品案例解析

GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
如果你想靠AI翻身,你先需要一个靠谱的工具!

工厂接口定义

复制代码 代码如下:

/// <summary>
    /// 工厂接口定义
    /// </summary>
    /// <remarks>
    ///     TTarget : abstract product type
    ///     TSource:  concrete product type
    /// </remarks>
    public interface IFactory
    {
        #region config and register type mapping

        /// <summary>
        /// 如果需要同时加载配置文件中定义的映射关系,可以按照SRP的原则定义独立的配置类型。
        /// 由该配置类型调用这两个接口为Factory加载配置信息
        /// </summary>

        IFactory RegisterType<TTarget, TSource>();  // fluent interface
        IFactory RegisterType<TTarget, TSource>(string name);   // fluent interface

        #endregion

        #region factory method

        TTarget Create<TTarget>();
        TTarget Create<TTarget>(string name);

        #endregion
    }

注册类

复制代码 代码如下:

public sealed class TypeRegistry
    {
        readonly string DefaultNmae = Guid.NewGuid().ToString();
        IDictionary<Type, IDictionary<string, Type>> registry = new Dictionary<Type, IDictionary<string, Type>>();
        public void RegisterType(Type targetType,Type sourceType)
        {
            RegisterType(targetType, sourceType, DefaultNmae);
        }
        public void RegisterType(Type targetType, Type sourceType,string name)
        {
            if (targetType == null) throw new ArgumentNullException("targetType");
            if (sourceType == null) throw new ArgumentNullException("sourceType");
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
            IDictionary<string, Type> subDictionary;

            if (!registry.TryGetValue(targetType, out subDictionary))
            {
                subDictionary = new Dictionary<string, Type>();
                subDictionary.Add(name, sourceType);
                registry.Add(targetType, subDictionary);
            }
            else
            {
                if (subDictionary.ContainsKey(name))
                    throw new DuplicateKeyException(name);
                subDictionary.Add(name, sourceType);
            }
        }
        public Type this[Type targetType, string name]
        {
            get
            {
                if (targetType == null) throw new ArgumentNullException("targetType");
                if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
                if (registry.Count() == 0)
                    return null;

                return (registry
                    .Where(x => x.Key == targetType)).FirstOrDefault().Value
                    .Where(x => string.Equals(name, x.Key))
                        .FirstOrDefault().Value;
            }
        }

        public Type this[Type targetType]
        {
            get { return this[targetType, DefaultNmae]; }
        }

    }

工厂类

复制代码 代码如下:

public class Factory : IFactory
    {
        protected TypeRegistry registry = new TypeRegistry();

        #region IFactory Members

        public IFactory RegisterType<TTarget, TSource>()
        {
            registry.RegisterType(typeof(TTarget), typeof(TSource));
            return this;
        }

        public IFactory RegisterType<TTarget, TSource>(string name)
        {
            registry.RegisterType(typeof(TTarget), typeof(TSource), name);
            return this;
        }

        public TTarget Create<TTarget>()
        {
            return (TTarget)Activator.CreateInstance(registry[typeof(TTarget)]);
        }

        public TTarget Create<TTarget>(string name)
        {
            return (TTarget)Activator.CreateInstance(registry[typeof(TTarget), name]);
        }

        #endregion
    }

调用

复制代码 代码如下:

[TestMethod]
        public void CreateInstance()
        {
            var factory = new Factory()
                .RegisterType<IFruit, Apple>()
                .RegisterType<IFruit, Orange>("o")
                .RegisterType<IVehicle, Bicycle>()
                .RegisterType<IVehicle, Bicycle>("a")
                .RegisterType<IVehicle, Train>("b")
                .RegisterType<IVehicle, Car>("c");

            Assert.IsInstanceOfType(factory.Create<IFruit>(), typeof(Apple));
            Assert.IsInstanceOfType(factory.Create<IFruit>("o"), typeof (Orange));

            Assert.IsInstanceOfType(factory.Create<IVehicle>(), typeof(Bicycle));
            Assert.IsInstanceOfType(factory.Create<IVehicle>("a"), typeof(Bicycle));
            Assert.IsInstanceOfType(factory.Create<IVehicle>("b"), typeof(Train));
            Assert.IsInstanceOfType(factory.Create<IVehicle>("c"), typeof(Car));
        }

其实精髓还是在于注册类的一个类似assembly的功能,通过字典的方式,封装,然后通过泛型来比对实现,或者通过配置文件传参数过来实现出一个新的实例化

里面注意连贯接口,泛型,等操作

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空