I recently discovered SOLID principles and i'm trying to learn how to properly apply them. I have an application that had a huge interface:
public interface NotificationService {
public void sendNewProjectRequestNotificationToDesigners(String projectId);
public void sendNewProjectAcceptedNotificationToCustomer(String projectId);
public void sendNewProjectRejectedNotificationToCustomer(String projectId);
...
// and aboud 30 more methods like that
// specifying the type of notification and who is going to be sent to
}
This clearly violates:
- SRP: so many different kind of notifications
- OCP: Every new notification i just modified the class
- ISP: In every single class that injects a NotificationService, will only want 1 of all the methods.
- DIP: The clients of this class need to be aware of the exact method to use.
so i began my refactoring and came up with:
public interface NotificationSender {
public void sendNotification(String projectId);
}
public class NewProjectRequestNotificationSender implements NotificationSender {
public void sendNotification(String projectId) {
...
}
}
// and many other classes, one for each method in the original interface
Now to apply Dependency Injection, i declared each class that implements NotificationSender as a @Bean with a qualifier name.
And the classes that require a specific notification sender, the dependency is injected with @Qualifier.
Question 1: By having to use @Qualifier in the clases that require a specific NotificationSender, hence these clases are still forced to know details like the qualifier name of the given NotificationSender, am i still violating Dependency Inversion Principle?
Question 2: Is there a better approach?