t*********e 发帖数: 630 | 1 Wikipedia 谈到 dependency injenction 的缺点:
1. Dependency injection can make code difficult to trace (read) because it
separates behavior from construction. This means developers must refer to
more files to follow how a system performs.[citation needed]
2. Dependency injection typically requires more lines of code to accomplish
the same behavior legacy code would.[citation needed]
3. Dependency injection diminishes encapsulation by requiring users of a
system to know how it works and not merely what it does.[5]
4. Dependency injection increases coupling by requiring the user of a
subsystem to provide for the needs of that subsystem.[6]
1 & 2 没有问题。3 & 4 是什么情况?怎么感觉 DI 不要求客户了解 dependency 的细
节?
On the contrary, it increases encapsulation and decreases coupling? |
t*********e 发帖数: 630 | 2 后面接着讲到 injection 的三种方式,constructor injection, setter injection
and interface injection. For instance,
Class Client {
private Service service;
public void setService(Service service) {
this.service = service;
}
}
Or
Class Client {
private Service service;
public Client(Service service) {
this.service = service
}
}
在 JEE 里面, 好像一行就解决了. 不用什么 constructor 或 setter method.
Class Client {
@Inject
private Service service;
}
怎么跟 wiki 上面说的不一样?
accomplish
【在 t*********e 的大作中提到】 : Wikipedia 谈到 dependency injenction 的缺点: : 1. Dependency injection can make code difficult to trace (read) because it : separates behavior from construction. This means developers must refer to : more files to follow how a system performs.[citation needed] : 2. Dependency injection typically requires more lines of code to accomplish : the same behavior legacy code would.[citation needed] : 3. Dependency injection diminishes encapsulation by requiring users of a : system to know how it works and not merely what it does.[5] : 4. Dependency injection increases coupling by requiring the user of a : subsystem to provide for the needs of that subsystem.[6]
|
z****e 发帖数: 54598 | 3 第一种和第二种都是不用reflection
并用xml配置的
第三种是直接annotation,实现用了reflection
reflection可以直接访问私有变量
然后塞入reference
以前没有annotation的时候,第三种需要写到xml里面去
这中间有一个历史发展的过程,所以不同时期都不太一样
不过越来越简单倒是没错,所以你用最后一种就好了 |
t*********e 发帖数: 630 | 4 谢谢。
DI 减少了很多 new 的使用,挺好的。
【在 z****e 的大作中提到】 : 第一种和第二种都是不用reflection : 并用xml配置的 : 第三种是直接annotation,实现用了reflection : reflection可以直接访问私有变量 : 然后塞入reference : 以前没有annotation的时候,第三种需要写到xml里面去 : 这中间有一个历史发展的过程,所以不同时期都不太一样 : 不过越来越简单倒是没错,所以你用最后一种就好了
|