国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#典型工廠化實現(xiàn)實例

2020-01-24 03:30:44
字體:
供稿:網(wǎng)友

工廠接口定義

復(fù)制代碼 代碼如下:

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

        /// <summary>
        /// 如果需要同時加載配置文件中定義的映射關(guān)系,可以按照SRP的原則定義獨立的配置類型。
        /// 由該配置類型調(diào)用這兩個接口為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
    }

注冊類

復(fù)制代碼 代碼如下:

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]; }
        }

    }

工廠類

復(fù)制代碼 代碼如下:

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
    }

調(diào)用

復(fù)制代碼 代碼如下:

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 景东| 麦盖提县| 柏乡县| 长沙市| 东宁县| 家居| 包头市| 九江县| 平舆县| 修水县| 平遥县| 黎平县| 邵武市| 永泰县| 彰化县| 义马市| 长岭县| 通渭县| 土默特左旗| 凌源市| 广水市| 天镇县| 商河县| 万盛区| 昭通市| 景泰县| 宁夏| 云林县| 隆尧县| 将乐县| 康定县| 建水县| 尚志市| 慈利县| 忻城县| 武穴市| 营山县| 涞源县| 吉隆县| 台中县| 古交市|