DI principle: Don’t depend on concrete implementation, rather depend on abstraction.
MouseKeyboardInterface
âš Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. âš
Text Elements
Mouse
Keyboard
Interfaces
WiredMouse
BluetoothMouse
WiredKeyboard
BluetoothKeyboard
child classes of the interface, Mouse and Keyboard.
Link to original
NOT Following DIP Principle đźš«
The MacBook
class directly creates instances of WiredKeyboard
and WiredMouse
classes. This creates a tight coupling between MacBook
and these specific implementations which can’t be changed in future.
Following DIP Principle âś…
In this code, MacBook specifications can be modified. Keyboard and Mouse can be of either wired or bluetooth or any other type for that matter and therefore, MacBook class isn’t tightly coupled with KeyBoard and Mouse as these are INTERFACE and not CONCRETE CLASSES.
The MacBook
class now depends on abstractions (Keyboard
and Mouse
) instead of concrete implementations. This allows you to easily swap out different implementations of keyboards and mice without modifying the MacBook
class. The high-level module (MacBook
) and low-level modules (WiredKeyboard
, WiredMouse
) are both dependent on abstractions, which follows the Dependency Inversion Principle.