我解释DI和IoC的方式有什么问题? [英] What is wrong in my way of explainning DI and 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 abstractclass(B)
or implementsinterface(B)
or create a object ofclass(B)
of any class in it, thenA
is said said to be dependent onB
. 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
orcoding through interface
in constructor or setter method parameter.Is this any way related withDI
/IOC
or this is only to achieveloose 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屋!