当前位置:  编程技术>.net/c#/asp.net

c#典型工厂化实现实例

    来源: 互联网  发布时间:2014-10-18

    本文导语:  工厂接口定义 代码如下:///     /// 工厂接口定义    ///     ///     ///     TTarget : abstract product type    ///     TSource:  concrete product type    ///     public interface IFactory    {        #region config and register type mapping ...

工厂接口定义

代码如下:

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

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

        IFactory RegisterType();  // fluent interface
        IFactory RegisterType(string name);   // fluent interface

        #endregion

        #region factory method

        TTarget Create();
        TTarget Create(string name);

        #endregion
    }

注册类

代码如下:

public sealed class TypeRegistry
    {
        readonly string DefaultNmae = Guid.NewGuid().ToString();
        IDictionary registry = new Dictionary();
        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 subDictionary;

            if (!registry.TryGetValue(targetType, out subDictionary))
            {
                subDictionary = new Dictionary();
                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()
        {
            registry.RegisterType(typeof(TTarget), typeof(TSource));
            return this;
        }

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

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

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

        #endregion
    }

调用

代码如下:

[TestMethod]
        public void CreateInstance()
        {
            var factory = new Factory()
                .RegisterType()
                .RegisterType("o")
                .RegisterType()
                .RegisterType("a")
                .RegisterType("b")
                .RegisterType("c");

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

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

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

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


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • <font color=red>又一个典型css实例
  • mongodb的典型使用场景
  • 关于一个安装Linux的典型问题
  • 推荐一本字典型工具书 Linux in a Nutshell
  • 编程技术其它 iis7站长之家
  • 100分:请各位前辈列举JAVA应用的典型案例.谢谢!
  • .net 线程同步 典型场景及问题
  • Java中典型的内存泄露问题和解决方法
  • 典型的三行二列居中高度自适应布局


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3