dependency-injection(依赖注入)
概念
sampleclassA
{
AInterface a;
A(){}
AMethod()//一个方法
{
a = new AInterfaceImpl();
}
}
这里面 使用new(对象创建)是一种硬编码,是代码耦合度变得很高,不方便测试.依赖注入简单的讲就是通过外界传入依赖来进行成员变量的初始化 依赖注入的三种实现方式
public interface IFather {
//method
}
public class Human {
IFather father;
public Human(IFather father) {
this.father = father;
}
}
public class Human {
IFather father;
public void setIFather(IFather father) {
this.father = father;
}
}
// 注入功能的interface
public interface InjectFinder {
void injectFinder(IFather father);
}
// 让我们的Human实现接口
public class Human implements InjectFinder {
IFather father;
public void injectFinder(IFather father) {
this.father = father;
}
}
参考: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |