欢迎您来到“名字什么都是浮云”的博客空间!

C#设计模式泛型注入

TSFac注入方式:


 

泛型接口工厂:

public class SFac<TInterface, TClass>
         where TInterface : class
         where TClass : TInterface
    {
        private static TInterface _instance;
        public static TInterface Instance
        {
            get
            {
                TInterface instanceTmp = Activator.CreateInstance<TClass>();//  TClass必须存在公共的构造函数
                if (_instance == null || _instance.GetType().FullName != instanceTmp.GetType().FullName)
                {
                    _instance = instanceTmp;
                }
                return _instance;
            }
        }
    }

泛型单例接口模式调用:

SFac<Interface, Class>.Instance.InterfaceMethod()

 

泛型类型工厂:

public class TFac<TClass> where TClass : class, new()
    {
        private static TClass _instance;
        public static TClass Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Activator.CreateInstance<TClass>();//  TClass必须存在公共的构造函数
                }
                return _instance;
            }
        }

    }

泛型单例对象模式调用:

TFac<Class>.Instance.Method()

以上方法已成熟使用过.

 

泛型服务注入(新):

注意:继承者必须存在有效的公共方法

/// <summary>
    /// 注入服务 (U:使用者,T:接口,R:继承者)
    /// 使用者与继承者必须一一对应
    /// </summary>
    public class SFac
    {
        private static Dictionary<Type, KeyValuePair<Type, Type>> data = new Dictionary<Type, KeyValuePair<Type, Type>>();
        private static List<string> typeUnique = new List<string>();

        private static SFac _instance;
        public static SFac Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Activator.CreateInstance<SFac>();//  TClass必须存在公共的构造函数
                }
                return _instance;
            }
        }

        public SFac() { }

        /// <summary>
        /// 注册服务 (参数:U,T,R)
        /// </summary>
        public void Add<U, T, R>()
        {
            var typeAllName = "Invoker:" + typeof(U).Name + ",interface:" + typeof(T).Name + ",extends:" + typeof(R).Name;
            if (typeUnique.Contains(typeAllName))
                return;
            typeUnique.Add(typeAllName);
            data[typeof(U)] = new KeyValuePair<Type, Type>(typeof(T), typeof(R));
        }
        /// <summary>
        /// 获得接口/模板  (参数:U,T)
        /// </summary>
        /// <returns>接口/模板</returns>
        public T Get<U, T>()
        {
            T res = default(T);
            if (data == null || data.Count == 0)
            {
                throw new Exception("不存在任何注册的服务!");
            }
           
            foreach (var use in data.Keys)
            {
                if (typeof(U) == use && typeof(T) == data[use].Key)
                {
                    try
                    {
                        res = (T)Activator.CreateInstance(data[use].Value);
                    }
                    catch 
                    {
                        throw new Exception("继承者"+ data[use].Value + "类,必须存在公共的构造方法!");
                    }
                    break;
                }
            }
            if (res == null)
            {
                throw new Exception("注册服务时使用者与继承者必须一对一!");
            }
            return res;
        }

    }
注入服务 (U:使用者,T:接口,R:继承者)
/// <summary>
    /// 接口-邮件服务
    /// </summary>
    public interface IMessageService
    {
        void SendMessage();
    }

    public interface IMusicService
    {
        void SendMessage();
    }

    /// <summary>
    /// 邮件服务
    /// </summary>
    public class EmailService : IMessageService
    {
        public void SendMessage()
        {
            Console.WriteLine("发送邮件...");
        }
    }
    /// <summary>
    /// 短信服务
    /// </summary>
    public class SMSService : IMessageService
    {
        public void SendMessage()
        {
            Console.WriteLine("发送短信...");
        }
    }
    
    /// <summary>
    /// 短信服务
    /// </summary>
    public class PhoneService : IMusicService
    {
        public void SendMessage()
        {
            Console.WriteLine("发送语音...");
        }
    }


    public class NotificationAndroid
    {
        
    }

    public class NotificationWeb
    {
       
    }

    public class NotificationIOS
    {
        
    }
示例中的接口和实现类以及调用类
public static void ApplicationStart()
        {
            //  应用程序启动时注册   注册时需要指定类型:使用者,接口,继承者
            SFac.Instance.Add<NotificationAndroid, IMessageService, EmailService>();
            //SFac.Instance.Add<EmailService, IMessagingService, EmailService>();
            SFac.Instance.Add<NotificationWeb,IMessageService, SMSService>();
            SFac.Instance.Add<NotificationIOS, IMusicService, PhoneService>();
        }
应用程序初始化代码
static void Main(string[] args)
        {
            

            ApplicationStart();

            //  调用时需要指定类型:使用者和接口
            SFac.Instance.Get<NotificationAndroid,IMessageService>().SendMessage();
            SFac.Instance.Get<NotificationWeb,IMessageService>().SendMessage();
            SFac.Instance.Get<NotificationIOS, IMusicService>().SendMessage();
            //SFac.Instance.Get<EmailService, IMessagingService>().SendMessage();
            Console.ReadKey();
        }
程序调用

 【原创】

posted @ 2017-04-25 15:29  名字什么都是浮云  阅读(1343)  评论(1编辑  收藏  举报