我们都知道控制反转和依赖注入是spring ioc的核心思想,也是面试经常被问到的话题,看到这篇文章又可以跟面试吹逼了,想要了解这两个就必须要了解设计原则中的依赖倒置原则
依赖倒置原则
指程序要依赖于抽象接口,不要依赖于具体实现。
举个栗子
假设公司需要开发一辆车,以轮胎为主体,就需要先设计轮胎,然后根据轮胎大小设计底盘,接着根据底盘设计车身,最后根据车身设计好整个汽车。这样就出现了一种"依赖关系":汽车依赖车身,车身依赖底盘,底盘依赖轮子;
这种设计表面看起来没什么问题,但是如果你要修改轮胎大小,那底盘、车身都要改,维护成本很大;
于是,我们倒过来设计,以汽车为主体,先把想要什么样的汽车设计出来,然后根据汽车设计出车身,接着根据车身设计底盘,最后根据底盘设计轮胎。
代码实现
设计初稿:以轮胎为主设计汽车
/**
* 轮胎类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:46 PM
* @Version 1.0
*/
public class Tire {
private int size;
public Tire() {
this.size = 60;
}
}
/**
* 底盘类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:46 PM
* @Version 1.0
*/
public class Bottom {
private Tire tire;
public Bottom() {
this.tire = new Tire();
}
}
/**
* 车身类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:45 PM
* @Version 1.0
*/
public class Body {
private Bottom bottom;
public Body() {
this.bottom = new Bottom();
}
}
/**
* 汽车类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:45 PM
* @Version 1.0
*/
public class Car {
private Body body;
public Car() {
this.body = new Body();
}
public void run(){
System.out.println("汽车组装完毕,开始跑起来...");
}
}
/**
* 测试类
* @Author: HuGoldWater
* @Date: 2019/6/16 11:00 PM
* @Version 1.0
*/
public class App {
public static void main(String[] args) {
final Car car = new Car();
car.run();
}
}
修改轮胎大小为100
如果领导有一天要求要给轮胎加个形状或者加个颜色啥的,又要全都改一遍,这样的设计违反了开闭原则
/**
* 轮胎类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:46 PM
* @Version 1.0
*/
public class Tire {
private int size;
public Tire(int size) {
this.size = 60;
}
}
/**
* 底盘类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:46 PM
* @Version 1.0
*/
public class Bottom {
private Tire tire;
public Bottom(int size) {
this.tire = new Tire(size);
}
}
/**
* 车身类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:45 PM
* @Version 1.0
*/
public class Body {
private Bottom bottom;
public Body(int size) {
this.bottom = new Bottom(size);
}
}
/**
* 汽车类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:45 PM
* @Version 1.0
*/
public class Car {
private Body body;
public Car(int size) {
this.body = new Body(size);
}
public void run(){
System.out.println("汽车组装完毕,开始跑起来...");
}
}
/**
* 测试类
* @Author: HuGoldWater
* @Date: 2019/6/16 11:00 PM
* @Version 1.0
*/
public class App {
public static void main(String[] args) {
final Car car = new Car(100);
car.run();
}
}
设计终稿:以汽车为主设计轮胎
符合依赖倒置、开闭原则,如果领导要求修改汽车轮胎大小或者形状,只需要修改Tire
即可,维护成本非常低,完美
/**
* 汽车类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:45 PM
* @Version 1.0
*/
public class Car {
private Body body;
public Car(Body body) {
this.body = body;
}
public void run(){
System.out.println("汽车组装完毕,开始跑起来...");
}
}
/**
* 车身类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:45 PM
* @Version 1.0
*/
public class Body {
private Bottom bottom;
public Body(Bottom bottom) {
this.bottom = bottom;
}
}
/**
* 底盘类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:46 PM
* @Version 1.0
*/
public class Bottom {
private Tire tire;
public Bottom(Tire tire) {
this.tire = tire;
}
}
/**
* 轮胎类
* @Author: HuGoldWater
* @Date: 2019/6/16 10:46 PM
* @Version 1.0
*/
public class Tire {
private int size;
public Tire(int size) {
this.size = 60;
}
}
/**
* 测试类
* @Author: HuGoldWater
* @Date: 2019/6/16 11:00 PM
* @Version 1.0
*/
public class App {
public static void main(String[] args) {
final Tire tire = new Tire(100);
final Bottom bottom = new Bottom(tire);
final Body body = new Body(bottom);
final Car car = new Car(body);
car.run();
}
}
总结:
依赖倒置原则:是由上层控制下层,而不是下层控制上层;控制反转就是基于这种思想来实现的,将控制权反转过来;上面还用了依赖注入中的构造器注入的方式。