我解释DI和IoC的方式有什么问题? [英] What is wrong in my way of explainning DI and IoC?

查看:55
本文介绍了我解释DI和IoC的方式有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,在一次采访中,我被问及春季的DI和IoC是什么。我的答复是:

Yesterday during an interview I was asked what DI and IoC in spring were. My reply was:


class(A)扩展抽象 class(B)或实现 interface(B)
或创建 class( B)中的任何类别,那么据说 A
,具体取决于 B 。注入此依赖关系,即以
构造函数或setter方法注入对象称为DI,在此过程中,对对象的创建
控制进入外部世界,如XML
配置,这种反转控制的是IoC。 DI不是必需的IOC。

when a class(A) extends abstract class(B) or implements interface(B) or create a object of class(B) of any class in it, then A is said said to be dependent on B. Injecting this dependency, i.e. injecting the object in costructor or in setter method is called DI and in this process control over creating object goes to the "outside world" like XML configuration, this inversion of control is IoC. DI is not necessary IOC. We can still have DI when there is no IOC.

面试官不同意我的意思,我在哪里错了?

The interviewer didn't agree with me - where was I wrong?

又一件事-


我们使用的是 Super class引用变量通过构造函数或setter方法参数中的接口进行编码。这是与$$$ DI相关的
吗? / IOC 还是只能实现松散耦合

As we used Super class reference variable or coding through interface in constructor or setter method parameter.Is this any way related with DI/IOC or this is only to achieve loose coupling?


推荐答案

IoC I 版本 o f C 控制)是一个抽象概念。这意味着对象不会直接创建从属对象,而是从对象范围之外获取的。

IoC (Inversion of Control) is an abstract concept. It means objects do not create dependent objects directly, it gets from outside of the object scope.

实现控制反转有几种基本技术。

There are several basic techniques to implement inversion of control.


  • 使用工厂模式

  • 使用服务定位器模式

  • 使用依赖注入(DI),例如


    • 构造函数注入

    • 参数注入

    • 设置注入

    • 接口注入

    • Using a factory pattern
    • Using a service locator pattern
    • Using Dependency Injection(DI), for example
      • Constructor injection
      • Parameter injection
      • Setter injection
      • Interface injection

      来源

      这篇关于我解释DI和IoC的方式有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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