a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c#典型工廠化實現實例

2020-01-24 03:30:44
字體:
來源:轉載
供稿:網友

工廠接口定義

復制代碼 代碼如下:

/// <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的功能,通過字典的方式,封裝,然后通過泛型來比對實現,或者通過配置文件傳參數過來實現出一個新的實例化

里面注意連貫接口,泛型,等操作

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲精品v| 久久网站热最新地址 | 久久精品在线 | 亚洲一区二区三区免费视频 | 国产一级免费 | 在线视频一二区 | 天堂一区二区三区 | 两性免费视频 | 九九热这里有精品 | 一区二区蜜桃 | 国产精品久久久久久久久久 | 四虎国产精品成人免费影视 | 中文字幕亚洲字幕一区二区 | 大黄网站在线观看 | 一级h片 | 日韩精品久久久久 | 欧美一级精品片在线看 | 国产精品视频一二三区 | 国产老女人精品毛片久久 | 日本一区二区三区四区 | 在线不卡视频 | 一级黄网 | 在线看免费观看日本 | a级黄色毛片免费观看 | www.久久精品 | av青青| 国产精品高清网站 | 久久久久亚洲视频 | 国产69精品99久久久久久宅男 | 一区二区三区观看视频 | 成人欧美在线视频 | 欧美性猛交一区二区三区精品 | 久久精品国产一区二区三区不卡 | www.日| 91蜜桃视频 | 99精品国产在热久久 | 欧美精三区欧美精三区 | 99亚洲视频| 欧美精品一区视频 | 亚洲成人另类 | 欧美字幕一区 |