zoukankan      html  css  js  c++  java
  • 设计模式——控制反转&依赖注入

    一、控制反转:

    从简单的代码示例入手:

        /// <summary>
        /// 邮件服务类
        /// </summary>
        public class EmailService
        {
            public string SendMessage()
            {
                return "发送通知邮件";
            }
        }
    
        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private EmailService service;
            public NotifycationSystem()
            {
                service = new EmailService(); //邮件通知类必须精确的知道创建和使用了哪种类型的服务,此处高耦合了。
            }
            public string InterestingEventHappened()
            {
                return service.SendMessage();
            }
        }
    共两个类,一个邮件服务类,一个邮件通知类,邮件通知类依赖于邮件服务类。邮件通知类必须精确的知道创建和使用了哪种类型的服务,此处高耦合了。

    改进一:在两代码块中引入抽象层,提取接口。

        /// <summary>
        /// 邮件服务接口
        /// </summary>
        public interface IMessageService
        {
            string SendMessage();
        }
    
        /// <summary>
        /// 邮件服务类
        /// </summary>
        public class EmailService : IMessageService
        {
            public string SendMessage()
            {
                return "发送通知邮件";
            }
        }
    
        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private IMessageService service;//邮件通知类保存服务实现的接口
            public NotifycationSystem()
            {
                service = new EmailService(); 
            }
            public string InterestingEventHappened()
            {
                return service.SendMessage();
            }
        }
    
    上面将依赖具体实现改为了依赖接口,减少了部分耦合。但是邮件服务类还是在邮件通知类内实例化的,也就是说邮件通知类还是要完全知道邮件服务类的具体细节。

    改进二:将选择抽象实现的责任移到服务消费者类的外部。

        /// <summary>
        /// 第二层抽象: 服务定位器
        /// </summary>
        public interface IServiceLocator
        {
            IMessageService GetMessageService();
        }
    
        /// <summary>
        /// 第一层抽象:邮件服务接口
        /// </summary>
        public interface IMessageService
        {
            string SendMessage();
        }
    
        /// <summary>
        /// 邮件服务类
        /// </summary>
        public class EmailService : IMessageService
        {
            public string SendMessage()
            {
                return "发送通知邮件";
            }
        }
    
        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private IMessageService service;//邮件通知类保存服务实现的接口。
            public NotifycationSystem(IServiceLocator locator)
            {
                service = locator.GetMessageService();//实现依赖关系被转移到类外。
            }
            public string InterestingEventHappened()
            {
                return service.SendMessage();
            }
        }
    
    扩展一:弱类型服务定位器。

    /// <summary>
        /// 第二层抽象: 服务定位器
        /// </summary>
        public interface IServiceLocator
        {
            object GetService(Type serviceType);
        }
    
        /// <summary>
        /// 第一层抽象:邮件服务接口
        /// </summary>
        public interface IMessageService
        {
            string SendMessage();
        }
    
        /// <summary>
        /// 邮件服务类
        /// </summary>
        public class EmailService : IMessageService
        {
            public string SendMessage()
            {
                return "发送通知邮件";
            }
        }
    
        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private IMessageService service;//邮件通知类保存服务实现的接口。
            public NotifycationSystem(IServiceLocator locator)
            {
                service = (IMessageService)locator.GetService(typeof(IMessageService));//实现依赖关系被转移到类外。
            }
            public string InterestingEventHappened()
            {
                return service.SendMessage();
            }
        }

    弱类型服务定位器使得这种模式更加灵活,因为他允许请求任意类型的服务类型。采用Type类型的参数,并返回一个非类型化的示例,也就是一个object类型对象。

    扩展二:泛型方法。

        /// <summary>
        /// 第二层抽象: 服务定位器
        /// </summary>
        public interface IServiceLocator
        {
            T GetService<T>();//泛型接口
            object GetService(Type serviceType);
        }
    
        /// <summary>
        /// 第一层抽象:邮件服务接口
        /// </summary>
        public interface IMessageService
        {
            string SendMessage();
        }
    
        /// <summary>
        /// 邮件服务类
        /// </summary>
        public class EmailService : IMessageService
        {
            public string SendMessage()
            {
                return "发送通知邮件";
            }
        }
    
        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private IMessageService service;//邮件通知类保存服务实现的接口。
            public NotifycationSystem(IServiceLocator locator)
            {
                service = locator.GetService<IMessageService>();//实现依赖关系被转移到类外。
            }
            public string InterestingEventHappened()
            {
                return service.SendMessage();
            }
        }
    泛型方法,让依赖反转代码看上去更加高效优雅。

    二、依赖注入:

    1.构造函数注入:

        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private IMessageService _service;
            public NotifycationSystem(IMessageService service)//构造函数注入
            {
                _service = service;
            }
            public string InterestingEventHappened()
            {
                return _service.SendMessage();
            }
        }
    2.属性注入:

        /// <summary>
        /// 邮件通知类
        /// </summary>
        public class NotifycationSystem
        {
            private IMessageService MessageService { get; set; }
            public string InterestingEventHappened()
            {
                if (MessageService == null)
                {
                    throw new InvalidOperationException("服务类型为赋值!");
                }
                return MessageService.SendMessage();
            }
        }




  • 相关阅读:
    网站前台性能优化教程
    解决Jboss打开run.bat时闪退不能启动的方法
    如何讲解自己开发的程序
    数据库调优教程汇总
    数据库调优教程(十三) MySQL数据库其他优化方法
    数据库调优教程(十二) 优化sql语句
    数据库调优教程(十一) 设计一张漂亮的表
    数据库调优教程(十) 【精华章节】解决like ’%str’ 时索引不被使用的4种方法
    数据库调优教程(九) 添加了索引但不被使用的几种常见可能
    Redis Cluster 实践
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/2802204.html
Copyright © 2011-2022 走看看