- Expert Android Programming
- Prajyot Mainkar
- 295字
- 2021-07-08 10:29:15
Dependency Inversion Principle
The Dependency Inversion Principle states that:
1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
2. Abstractions should not depend upon details. Details should depend upon abstractions.
The best way to explain this principle is by giving an example. Let's assume we have a worker class that is a low level class and a Manager class that is a high level class. The Manager class contains many complex functionalities which it implements along with the Worker class, so the classes will look something like this:
class Worker { public void work() { // ....working } } class Manager { //--Other Functionality Worker worker; public void setWorker(Worker w) { worker = w; } public void manage() { worker.work(); } }
Here the Manager class has been implemented and is directly associated with the Worker class due to which changes in the Worker class directly affect the Manager class.
If we have to add another class which would be a parent of the Worker class, and the Worker class does similar work to that of the Manager class, it will require lots of changes.
To make it easier to add the Manager class, we will use interfaces:
interface IWorker { public void work(); } class Worker implements IWorker{ public void work() { // ....working } } class SuperWorker implements IWorker{ public void work() { //.... working much more } } class Manager { IWorker worker; public void setWorker(IWorker w) { worker = w; } public void manage() { worker.work(); } }
Now the both the worker and SuperWorker class implement the IWorker, while the Manager class directly uses the IWorker to complete its functionality by which changes to the Worker and SuperWorker do not affect the Manager class.
- VMware View Security Essentials
- Moodle Administration Essentials
- MySQL數(shù)據(jù)庫應(yīng)用與管理 第2版
- 華為HMS生態(tài)與應(yīng)用開發(fā)實戰(zhàn)
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓(xùn)版)
- 名師講壇:Spring實戰(zhàn)開發(fā)(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- Go語言開發(fā)實戰(zhàn)(慕課版)
- Learning Image Processing with OpenCV
- 從零開始學(xué)算法:基于Python
- Moodle 3.x Developer's Guide
- C#程序開發(fā)參考手冊
- 少年小魚的魔法之旅:神奇的Python
- 利用Python駕馭Stable Diffusion:原理解析、擴展開發(fā)與高級應(yīng)用(智能系統(tǒng)與技術(shù)叢書)
- 算法學(xué)習(xí)與應(yīng)用從入門到精通
- Hadoop MapReduce v2 Cookbook(Second Edition)