当前位置:网站首页>Dependency Inversion Principle
Dependency Inversion Principle
2022-06-24 00:03:00 【One night, Nara mountain was at odds】
The dependency inversion principle is that programs depend on abstract interfaces , Don't rely on concrete implementation . In short, it requires programming the abstraction , Do not program the implementation , This reduces the coupling between the customer and the implementation module .
For not considering abstraction , The high level relies directly on the low-level code , Requirement changes create type problems

according to DIP Redesign the code , Completed high level , The relationship between the bottom , The relationship between the bottom layers .

//DishDeal Interface
public interface DishDeal{
void Deal();
}
// Qishanmian implements the interface ( Bottom )
public class QishanNoddle implements DishDeal{
String name;
@Override
public void Deal() {
}
}
// Clear water surface implementation interface
public class QingshuiNoddle implements DishDeal{
String name;
@Override
public void Deal() {
}
}
// Shandong cake implementation interface
public class ShanDongBing implements DishDeal{
String name;
@Override
public void Deal() {
}
}
//ChefCooking Interface
public interface ChefCooking
{
void cooking(DishDeal dishdeal);
}
//CheCooking Implementation class
public class Cooker implements ChefCooking{
public void cooking(DishDeal dishdeal) {
dishdeal.Deal();
}
}
// Ordering system ( high-level )
class Client {
public static void main(String[] args) {
ChefCooking Zhang;
// An interface has multiple implementation classes , according to new To determine the implementation method of the call .
DishDeal QingShuiNoodle=new QingshuiNoddle();
Zhang.cooking(QingShuiNoodle);// Clear water surface practice
DishDeal QishanNoodle =new QishanNoddle();
Zhang.cooking(QishanNoodle);// Qishan noodles
}
}
Sum up :
① Abstractions should not depend on details , Details should depend on abstractions . Details are stable and abstractions are unstable .
Define multiple abstract classes , Each abstract class has several implementation methods , Then an abstract class should call another abstract class , Instead of directly calling its concrete methods , Because the details are unstable .
Details depend on abstraction, which means , The underlying code should not call each other directly , Instead, it calls the abstract indirectly .
② High level modules should not depend on the underlying modules , Instead, both sides rely on abstraction , That is, high-level dependency abstraction , To complete the function , The bottom layer also relies on abstraction , Ensure that the high-level code does not change with the low-level code .
边栏推荐
- Docker redis cluster configuration
- Synthetic big watermelon games wechat applet source code / wechat game applet source code
- What are the good solutions for industrial control safety of production line
- 工作中一些常用的工具函数
- 【FreeRTOS】07 二值信号量、计数信号量
- Quelques fonctions d'outils couramment utilisées au travail
- . Net
- 节流和防抖
- 【红绿灯识别】基于matlab GUI红绿灯识别【含Matlab源码 1908期】
- How to achieve energy-saving and reasonable lighting control in order to achieve the "double carbon" goal
猜你喜欢

fatal: The upstream branch of your current branch does not match the name of your current branch.

extern、struct等关键字

Docker Deployment redis

Docker deploy redis

The easycvr program started abnormally as a service, but the process started normally. What is the reason?

2.摄像机标定

Improvement of DC power distribution with open hall current sensor

B2B transaction management system of electronic components industry: improve the data-based driving ability and promote the growth of enterprise sales performance

物联网卡设备接入EasyCVR,如何查看拉流IP以及拉流时间?

Detailed explanation of index invalidation caused by MySQL
随机推荐
NLP-D58-nlp比赛D27&刷题D14&读论文&mathtype
Save: software analysis, verification and test platform
Perfectionist win10 installation guide
Synthetic big watermelon games wechat applet source code / wechat game applet source code
Six complete open source projects, learning enough at a time
2.摄像机标定
What is the difference between concurrency and parallelism?
This high imitation millet mall project is amazing
Nice input edit box
【面试经验包】面试被吊打经验总结(一)
【图像检测显著图】基于matlab失真提示鱼眼图显著图计算【含Matlab源码 1903期】
Server2022 activation
量化投资模型——高频交易做市模型相关(Avellaneda & Stoikov’s)研究解读&代码资源
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
Classical Chinese can be programmed???
Solve the problem of project dependency red reporting
Setting method of bar code local segment data variable
生成所有可能的二叉搜索树
国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知 石原子科技重磅推出
被同事坑到周末加班, 没见过把Redis用成这个鬼样子的。。。