控制反转IOC-inversion of controller

原始场景

一个行李箱的构建需要从轮子开始做起,如图所示:

Created with Raphaël 2.2.0 行李箱 箱体 底盘 轮子

原来的代码可能是这样的:(代码不严谨,只是为了展示,望不计小节)

public class luggage{
	luggage{
		this.frame = new frame();
	}
}
public class frame{
	frame(){
		this.bottom = new bottom();
	}
}
public class bottom{
	bottom(){
		this.tire = new tire();
	}
}
public class tire{
	tire(){
	}
}

如果产品突然说要加一个参数,轮子的大小应该是可以改动的,那么代码可能要改成这样子

public class luggage{
	luggage(int size){
		this.frame = new frame(size);
	}
}
public class frame{
	frame(int size){
		this.bottom = new bottom(size);
	}
}
public class bottom{
	bottom(int size){
		this.tire = new tire(size);
	}
}
public class tire{
	tire(int size){
		this.size = size;
	}
}

如果存在一个类被几千个类所依赖的,那要修改的代码将是不可预估的,也就是说,这种设计代码的方式,代码的耦合度非常高。这时候,就需要控制反转

用依赖注入DI实现控制反转

Created with Raphaël 2.2.0 轮子 底盘 箱体 箱子
public luggage{
	luggage(frame){
		this.frame = frame;//一开始就如此设计,任何参数对于上层来说都是透明的
	}
}

最后如此调用代码

int size = 30;
tire t = new tire(size);
bottom b = new bottom(t);
frame f = new frame(b);
luggage l = new luggage(f);
l.move();//现在箱子可以自由推动辣

上面这种思想,是依赖倒置原则

依赖注入的方式

DI 注入依赖最主流的方式,主要有以下几种:

  • setter
  • interface
  • constructor 我们刚才实现的就是这种方式
  • annotation

IOC容器的好处

  • 避免在各处使用new来创建类,并且可以做到统一维护
  • 创建实例的时候可以不需要了解其中的细节
  • 反射+工厂模式的合体,满足开闭原则
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值