Answer the question
In order to leave comments, you need to log in
Which approach to use?
Every time I need to write some service, I am faced with the following question:
Suppose you need a service that performs user notifications. You can notify, for example, in Slack, via SMS and Telegram. I see 2 solutions
1. Create some abstract Notifier class with an abstract notify method,
and for each service (Slack, SMS, Telegram) create child classes that inherit Notifier (SlackNotifier, SMSNotifier, TelegramNotifier), in which to implement the nofity method in their own way.
After that, get the necessary implementation to the controller from the DI container, depending on the configuration.
2. Create a very specific Notifier, and in it, as a dependency, instantiate SlackDriver, SMSDriver or TelegramDriver that implement a common interface with the notify method, and delegate execution to the instantiated driver in the Notifier::notify() method:
public function notify() {
$this->driver->notify();
}
Answer the question
In order to leave comments, you need to log in
Notifications - in the real world, this is a very complex topic, with a lot of underwater rakes and regularly changing requirements. Let's see what entities we have here in terms of the Single Responsibility Principle (SRP):
1. NotificationChannel. This is it: Email, SMS, Telegram, Slack, VK, etc. It is his responsibility to send the message in a certain way (a certain text to a certain address). I would write its interface like this:
interface NotificationChannel {
void send(String to, String text);
}
interface Notifier {
void notify(User user, Event event);
}
Here the first abstraction is UserNotifier, a service that notifies the user. It hides notification methods. Something like one Notify method. It can be implemented through a DI container.
And it is internally implemented with knowledge of the notification delivery channels. But even here I would abstract from specific implementations through Slack, SMS, Telegram. The connection of specific implementations should be handled by the application, not the implementation of the UserNotify service, the connection is also via DI.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question