依赖注入的实际用例是什么? [英] what is the real use case of dependency injection?

查看:108
本文介绍了依赖注入的实际用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解依赖注入,但是我真正不了解的是,依赖注入的用途是什么.

I understand dependency injection but what i really don't understand is, what is the use of dependency injection.

如前所述此处,这有助于轻松地测试代码( ,但这是一种非常有用的测试技术,因为它允许对依赖项进行模拟或存根.),但是现在有很多模拟框架,例如 powermockito 与为什么进行依赖注入相比,哪种方法可以很好地完成这些工作?

As it is mentioned here this helps in testing your code easily (It's a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.) , but now a days there are lot of mocking frameworks like Mockito, powermockito which can do these jobs quite well than why dependency injection?

如果有人可以用代码解释,那就太好了.

It would be great if somebody could explain with code.

谢谢.

推荐答案

DI作为一种技术的主要用途是:

The primary uses for DI as a technique are :

它导致易于测试的代码(使用Mocking).这意味着,为了使用模拟框架(Mockito等),您应该使用更多的DI.如果您不使用DI并编写直接实例化对象的代码-那么实际上您将无法使用Mockito来模拟依赖项.

It leads to a code that is easier to test ( using Mocking ). This means that in order to use mocking frameworks ( Mockito etc ) , you should be using more of DI. If you dont use DI and write code that instantiates objects directly - then practically you cannot use Mockito to mock your dependencies.

让我们说您编写代码来演奏管弦乐队.您的主要班级取决于很多其他班级(也许别人也写过).

Lets say you write code to play an Orchestra. Your main class depends upon so many other classes ( that other people wrote perhaps ).

可以说您是这样写的:

public class Orchestra {
  private Drums drum;
  private Violin violin;
  private Guitar guitar;

public Orchestra() {
    drum = new Drum();
    violin = new Violin();
    guitar = new Guitar();
}

public Music play(){
    // use above in some way to run your orchestra
    // start with violin 
    // add some guitar and then bring in the drums too
}

}

现在,您要确保play中的逻辑正确运行.当您上课时,您会看到音乐不是您所期望的. (也许鼓声是从一开始就开始的).您想测试代码play的逻辑.您在这里怎么办?您无法控制自己的依赖项.您不知道Drum代码或play()中您自己的逻辑是否存在问题.

Now you want to ensure that your logic in play works accurately. When you run your class you see the music is not what you are expecting. ( maybe the drummming is starting right at the start ). You want to test the logic of your code play. How would you do that here ? You have no control over your dependencies. You dont know if there was an issue with code of Drum or your own logic in play().

您想在此处模拟 Drum.但是您不能轻易做到这一点.因为您的代码不使用DI.它直接在内部实例化Drum.

You would want to mock out Drum here. But you cannot do that easily. Because your code does not use DI. Its instantiating the Drum directly inside.

现在让我们使用DI并看看它如何提供帮助.

Now lets use DI and see how it helps.

public class Orchestra {
    private Drums drum;
    private Violin violin;
    private Guitar guitar;

    public Orchestra(Drums d,Violin v, Guitar g ) {
        drum = d;
        violin = v;
        guitar = g;
    }

    public Music play(){
        // use above in some way to run your orchestra
    }

}

使用此代码,可以轻松在测试中模拟Drum.

With this code , it becomes easy to mock out Drum in your test.

class TestOrchestra {

    public void testPlay(){
        Drum mockDrum = mock(Drum.class);
        Violin mockViolin = mock(Violin.class);
        Guitar mockGuitar = mock(Guitar.class);
        // add mock behaviour to above , here you control precisely what these dependencies will do inside your code

        Orchestra orch = new Orchestra(mockDrum, mockViolin,mockGuitar);
        // now play and test your logic

    }

}

使用DI的第二个优点是,它无需更改整个代码即可帮助改变程序较大部分的实现.

The second advantage of using DI is that it helps vary implementations of bigger parts of your program without needing to go through your entire code.

再次-参照上述内容,可以说您一直在使用特定类型的吉他(由您的代码在内部实例化)弹奏乐队.

Again - with reference to above , lets say you have been playing your orchestra using a particular type of guitar ( which is instantiated internally by your code).

现在您想换一个全新的电吉他,如果没有DI,您将必须打开Orchestra类,检查在哪里创建Guitar并更改该行代码.您必须确保不会无意中更改代码的其他部分,并测试整个Orchestra以确保一切正常.

Now you want to change to an brand new electric guitar.Without DI , you would have to open up your Orchestra class , check where you create the Guitar and change that line of code. You would have to ensure that other parts of your code are not changed inadvertently and test entire Orchestra to make sure things work correctly.

使用DI可以避免所有这些情况.您只需将新的Guitar对象注入到构造函数中即可.而已.因为您已经单独测试了新的Guitar对象(并且它满足了interface Guitar的约定-您可以放心,注入新的Guitar不会破坏您的Orchestra代码.这是一个更好的改进您的乐团.

With DI , you could avoid all this. You just inject the new Guitar object into your constructor. Thats it. Because you have tested the new Guitar object on its own ( and it fulfills the contract of the interface Guitar - you can be rest assured that your Orchestra code is not broken by injecting this new Guitar. Thats quite a better improving your orchestra.

这篇关于依赖注入的实际用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆