
面试题:什么是控制反转和依赖注入?
这种设计表面看起来没什么问题,但是如果你要修改轮胎大小,那底盘、车身都要改,维护成本很大;
发布日期:2021-05-07 04:49:53
浏览次数:23
分类:精选文章
本文共 3797 字,大约阅读时间需要 12 分钟。
我们都知道控制反转和依赖注入是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(); }}
总结:
依赖倒置原则:是由上层控制下层,而不是下层控制上层;控制反转就是基于这种思想来实现的,将控制权反转过来;上面还用了依赖注入中的构造器注入的方式。
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年04月08日 09时56分27秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
《你是在做牛做马还是在做主管》- 读书笔记
2021-05-09
ASP.NET Core on K8S学习之旅(12)Ingress
2021-05-09
重新温习软件设计之路(4)
2021-05-09
《刷新》:拥抱同理心,建立成长型思维
2021-05-09
MVC3+NHibernate项目实战(二) :数据库访问层
2021-05-09
Flask入门
2021-05-09
MySQL数据库与python交互
2021-05-09
python如何对字符串进行html转义与反转义?
2021-05-09
开发小白也毫无压力的hexo静态博客建站全攻略 - 躺坑后亲诉心路历程
2021-05-09
java例题_24 逆向输入数字
2021-05-09
不管人生怎么走,都需要实时回头看看
2021-05-09
golang基础--类型与变量
2021-05-09
Bitcoin区块链攻击方式
2021-05-09
.NetCore外国一些高质量博客分享
2021-05-09
Mysql的基本操作(一)增、删、改
2021-05-09
解决WebRTC中不同的浏览器之间适配的问题
2021-05-09
如何快速在odoo中创建自己的菜单
2021-05-09