avatar
Dependency Injection# Java - 爪哇娇娃
t*e
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?
avatar
t*e
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]

avatar
z*e
3
第一种和第二种都是不用reflection
并用xml配置的
第三种是直接annotation,实现用了reflection
reflection可以直接访问私有变量
然后塞入reference
以前没有annotation的时候,第三种需要写到xml里面去
这中间有一个历史发展的过程,所以不同时期都不太一样
不过越来越简单倒是没错,所以你用最后一种就好了
avatar
t*e
4
谢谢。
DI 减少了很多 new 的使用,挺好的。

【在 z****e 的大作中提到】
: 第一种和第二种都是不用reflection
: 并用xml配置的
: 第三种是直接annotation,实现用了reflection
: reflection可以直接访问私有变量
: 然后塞入reference
: 以前没有annotation的时候,第三种需要写到xml里面去
: 这中间有一个历史发展的过程,所以不同时期都不太一样
: 不过越来越简单倒是没错,所以你用最后一种就好了

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。