0% completed
In the previous lesson, we saw how a tightly coupled design violates the Dependency Inversion Principle (DIP). To follow DIP, we need to introduce abstractions and make both high-level and low-level modules depend on them, rather than depending directly on each other.
We’ll start by creating an abstraction for sending notifications. This abstraction will be an interface called NotificationSender
, which defines a method for sending notifications.
Next, we’ll implement different notification services, such as EmailService
and SMSService
, that depend on the NotificationSender
interface.
Now, the low-level modules (EmailService
and SMSService
) implement the NotificationSender
interface, following the principle that details should depend on abstractions.
We’ll refactor the NotificationService
to depend on the NotificationSender
interface instead of directly on a specific implementation. This will decouple the high-level module from the low-level details.
Now, we can easily switch between different implementations of NotificationSender
(e.g., EmailService
and SMSService
) without modifying the NotificationService
.
send()
method for sending notifications.NotificationSender
interface, providing specific implementations for sending emails and SMS.NotificationSender
interface, making it flexible and decoupled from specific implementations.NotificationService
with both EmailService
and SMSService
.NotificationService
class depends on the NotificationSender
interface, not on specific implementations.EmailService
and SMSService
implement the NotificationSender
interface, allowing them to follow a common contract.NotificationSender
interface without modifying the existing NotificationService
......
.....
.....