Inversion of Control
1. Hollywood's principle: Don't call me, I'll call you
2. Instead of lookup the resource we want. we leave it to the framework
Traditional 'pull approach'
Context ctx = new InitialContext();
AccountEJB accountEJB = (AccountEJB) ctx.lookup("java:comp/env/ejb/AccountEJB");
Drawback:
1. client codes tied to a specific implementation
2. codes are difficult to test (must be tested within the container)
IoC Approach:
private Account accountManager;
public void setAccountManager(Account account) {
this.accountManager = account;
}
Addvantage:
1. client code does not need to deal with component discovery
2. client code isn't tied to any specific implementations
3. codes can Unit Testing?individually with mock object
3 Types:
Type 1: interface injection?
dependency satisfied by interfaces it implemented
Type 2: setter injection
dependency satisfied by JavaBean properties and its setter exposed by a component
Type 3: constructor injection
dependedcy satisfied by the component's constructor
博客介绍了控制反转(IoC),遵循好莱坞原则,即让框架来调用。对比传统‘拉取方法’,指出其客户端代码与特定实现绑定、难测试的缺点。而IoC方法使客户端代码无需处理组件发现,不与特定实现绑定,可单独进行单元测试。还介绍了接口注入、setter注入和构造函数注入三种类型。
369

被折叠的 条评论
为什么被折叠?



