博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搬砖方法论:依赖注入的三种形式
阅读量:4147 次
发布时间:2019-05-25

本文共 3212 字,大约阅读时间需要 10 分钟。

  • 构造注入(Constructor Injection)
  • 方法注入(Method Injection)
  • 属性注入(Property Injection)又称为:Setter Injection

定义

Constructor Injection:指的是在类型的构造函数中,以传参的形式,将该类型的依赖关系需求,以静态的形式定义出来。

示例代码

using System;using UnityEngine;public class ConstructorInjection : MonoBehaviour{    // Start is called before the first frame update    void Start()    {        var description_0 = new Description_0();        var book_0 = new Book(description_0);//将 IDescription 依赖性需求,注入到Book中        book_0.Read();        var description_1 = new Description_1();        var book_1 = new Book(description_1);//将 IDescription 依赖性需求,注入到Book中        book_1.Read();    }}public class Book{    private readonly IDescription m_description;    public Book(IDescription description)    {        if (description == null)        {            throw new ArgumentNullException("description");        }        m_description = description;    }    public void Read()    {        m_description.ToContent();    }}public interface IDescription{    void ToContent();}public class Description_0 : IDescription{    public void ToContent()    {        Debug.Log("内容_0");    }}public class Description_1 : IDescription{    public void ToContent()    {        Debug.Log("内容_1");    }}

补充:

  • 由于构造注入中,构造函数中的参数就代表了类型对依赖需求的定义,而类型对依赖需求不应该有多重版本(重载),所以构造函数只应该有一种。
  • 不应该在构造函数中添加其他业务逻辑(除了防御性语句的判空操作),也不要对依赖需求对象进行任何处理。

优缺点

优点 缺点
简单 对不支持构造传参的运行方式支持不友好
可以明确的指出类型的依赖需求
能够保证依赖需求一定会注入
直观看出依赖需求的数量是否合理

使用优先级:高


定义

Method Injection:指的是通过方法参数的形式,向使用方提供需要的依赖对象。

示例代码

using UnityEngine;public class MethodInjection : MonoBehaviour{    void Start()    {        var people = new People();        people.Eat(new Hamburger());        people.Eat(new Dumplings());    }}public class People{    public void Eat(IFood food)    {        Debug.Log($"吃:{food.ToContent()}");    }}public interface IFood{    string ToContent();}public class Hamburger : IFood{    public string ToContent()    {        return "汉堡";    }}public class Dumplings : IFood{    public string ToContent()    {        return "饺子";    }}

补充:

  • 每次依赖的使用者不同或每次调用方使用的依赖对象不同时,使用方法注入是个不错的选择。
  • 不要对方法注入的依赖需求进行缓存操作。

优缺点

优点 缺点
可以在每次方法操作时都提供不能的依赖需求 适用范围有限
弥补构造注入中依赖需求爆炸的情况 依赖需求分散在不同方法中,不利于管理

使用优先级:中


定义

Property Injection:指的是通过对外公开的(pulic)属性,将依赖对象的需求注入到使用方。

示例代码

using UnityEngine;public class PropertyInjection : MonoBehaviour{    // Start is called before the first frame update    void Start()    {        var game = new Game();        game.Platform = new PC_Platform();        game.Play();        game.Platform = new Mobile_Platform();        game.Play();    }}public class Game{    public IPlatform Platform { get; set; }    public void Play()    {        var tempStr = $"游戏运行在:{Platform.ToContent()} 平台";        Debug.Log(tempStr);    }}public interface IPlatform{    string ToContent();}public class PC_Platform : IPlatform{    public string ToContent()    {        return "PC";    }}public class Mobile_Platform : IPlatform{    public string ToContent()    {        return "Mobile";    }}

补充:

  • 只有在开发通用函数库时,有默认的内置选项,并且该函数库对依赖对象属于非必要的前提下,才会使用属性注入方式。
    要是对依赖对象的需求范围小且持续时间段,应该使用方法注入,剩下的情况使用构造注入。
  • 当构造注入和方法注入都不能满足需求时,再慎重使用属性注入。

优缺点

优点 缺点
操作简单 适用范围有限
理解成本提高
会造成时序耦合
只适用于通用组件(例:日志系统正式与开发环境的切换)

使用优先级:低


总结:

从使用范围和优缺点来看,使用的优先级由高到低依次为:构造注入--->方法注入--->属性注入


更多文章详见主页:

转载地址:http://iajti.baihongyu.com/

你可能感兴趣的文章
有趣的排序——百度2017春招
查看>>
二叉树的最近公共祖先LCA
查看>>
数组中累加和为定值K的最长子数组长度
查看>>
素数对--腾讯2017校招编程
查看>>
JAVA集合--ArrayList实现原理
查看>>
synchronized与Lock
查看>>
数据库索引
查看>>
实现包含min,max,push,pop函数的栈
查看>>
实验2-6 字符型数据的输入输出
查看>>
实验3-5 编程初步
查看>>
实验4-1 逻辑量的编码和关系操作符
查看>>
实验5-2 for循环结构
查看>>
实验5-3 break语句和continue语句
查看>>
实验5-4 循环的嵌套
查看>>
实验5-5 循环的合并
查看>>
实验5-6 do-while循环结构
查看>>
实验5-7 程序调试入门
查看>>
实验5-8 综合练习
查看>>
第2章实验补充C语言中如何计算补码
查看>>
深入入门正则表达式(java) - 命名捕获
查看>>