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...