博主介绍: ✌博主从事应用安全和大数据领域,有8年研发经验,5年面试官经验,Java技术专家✌
Java知识图谱点击链接:体系化学习Java(Java面试专题)
💕💕 感兴趣的同学可以收藏关注下 ,不然下次找不到哟💕💕
依赖倒置原则
依赖倒置原则 Dependence Inversion Principle, DIP。
《设计模式之禅》第三章介绍到:
High level modules should not depend upon low level modules. Both should depend uporabstractions. Abstractions should not depend upon details, Details should depend uponabstractions.
这上面主要讲三个含义,这个是可以在面试中陈述的,说明依赖倒置原则的特点:
1、高层模块不应该依赖低层模块,两者都应该依赖其抽象
2、抽象不能依赖细节
3、细节应该依赖抽象
我们接下来介绍依赖倒置的三种写法
依赖倒置的三种写法
1、接口传递
package com.pany.camp.design.principle.lsp;
/**
*
* @description: 车子的抽象类
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: panyong
* @version: 1.0.0
* @createTime: 2023-05-31 13:26
*/
public abstract class Car {
/**
* 启动
*
* @since 1.0.0
* @param
* @return: void
* @author: panyong
* @version: 1.0.0
* @createTime: 2023-05-31 13:28
*/
abstract void firing();
/**
* 车子的牌子
*
* @since 1.0.0
* @param
* @return: String
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 13:36
*/
abstract String getName();
}
package com.pany.camp.design.principle.lsp;
/**
*
* @description: 法拉利
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 13:40
*/
public class Ferrari extends Car {
@Override
void firing() {
System.out.println("drive ferrari");
}
@Override
String getName() {
return "ferrari";
}
}
package com.pany.camp.design.principle.lsp;
/**
*
* @description: 保时捷
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 13:30
*/
public class Porsche extends Car {
@Override
void firing() {
System.out.println("drive porsche");
}
@Override
String getName() {
return "porsche";
}
}
package com.pany.camp.design.principle.lsp;
public class User {
public void drive(Car car) {
car.firing();
System.out.println("drive " + car.getName());
}
public static void main(String[] args) {
User user = new User();
// 我可以换成其他的方式,并没有改变代码的逻辑
user.drive(new Porsche());
user.drive(new Ferrari());
}
}
2、构造器传递
package com.pany.camp.design.principle.dip;
/**
*
* @description: 电脑
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:13
*/
public interface Computer {
/**
* 打游戏
*
* @since 1.0.0
* @param
* @return: void
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:14
*/
void playGame();
}
package com.pany.camp.design.principle.dip;
/**
*
* @description: 惠普
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:15
*/
public class Hp implements Computer {
@Override
public void playGame() {
System.out.println("I use Hp computer, start play game");
}
}
package com.pany.camp.design.principle.dip;
/**
*
* @description: 学生
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:15
*/
public class Student {
private Computer computer;
public Student(Computer computer) {
this.computer = computer;
}
/**
* 开始打游戏
*
* @since 1.0.0
* @param
* @return: void
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:17
*/
public void startPlayGame() {
this.computer.playGame();
}
/**
* 测试方法
*
* @since 1.0.0
* @param
* @param args
* @return: void
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:17
*/
public static void main(String[] args) {
Student student = new Student(new Hp());
student.startPlayGame();
}
}
3、set 方法传递在这里插入代码片
在构造器例子的基础上,新写一个 User,通过 set 方法传递。
package com.pany.camp.design.principle.dip;
/**
*
* @description: 学生
* @copyright: @Copyright (c) 2022
* @company: Aiocloud
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:15
*/
public class User {
private Computer computer;
public void setComputer(Computer computer) {
this.computer = computer;
}
/**
* 开始打游戏
*
* @since 1.0.0
* @param
* @return: void
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:17
*/
public void startPlayGame() {
this.computer.playGame();
}
/**
* 测试方法
*
* @since 1.0.0
* @param
* @param args
* @return: void
* @author: pany
* @version: 1.0.0
* @createTime: 2023-05-31 21:17
*/
public static void main(String[] args) {
User user = new User();
user.setComputer(new Hp());
user.startPlayGame();
}
}
💕💕 本文由激流丶创作,原创不易,感谢支持!
💕💕喜欢的话记得点赞收藏啊!