IoC 容器的职责
维基百科
• 维基百科(https://en.wikipedia.org/wiki/Inversion_of_control)
在Overview 小节中提到:
“Inversion of control serves the following design purposes:
IoC通常有以下几个设计目的:
• To decouple the execution of a task from implementation.
实现与执行的目的之间要产生解耦.
• To focus a module on the task it is designed for.
关注于模块,和任务赋予模块的功能. 就是说要关注于你设计上的最终目标, 而不是具体实现.
• To free modules from assumptions about how other systems do what they do and instead rely on contracts.
释放模块. 系统知道模块的运行方式但是不依赖某个契约.
比如 Java EE 的 JNDI, 尽管实现了组件的实现和组件进行解耦, 但是还是要依赖于像 lookup 这样的接口, 这就是契约了.
• To prevent side effects when replacing a module.
避免当模块取消是产生的边缘效应/副作用, 这里不太理解, 猜一下是当发生依赖变更时, 会产生一些副作用, 那么IoC的话能减少这个影响.
Inversion of control is sometimes facetiously referred to as the “Hollywood Principle:
Don’t call us, we’ll call you”.”
好莱坞原则: Don’t call us, we’ll call you.