Junior — Middle
Опишите, каким образом реализована система отправки уведомлений в вашем проекте
sobes.tech AI
Answer from AI
In my Java project, the notification sending system is implemented using the "Observer" pattern. There is a central notification service that manages a list of subscribers (listeners) and sends them messages when certain events occur.
An example of a simplified implementation:
interface NotificationListener {
void onNotify(String message);
}
class NotificationService {
private List<NotificationListener> listeners = new ArrayList<>();
public void subscribe(NotificationListener listener) {
listeners.add(listener);
}
public void unsubscribe(NotificationListener listener) {
listeners.remove(listener);
}
public void notifyAll(String message) {
for (NotificationListener listener : listeners) {
listener.onNotify(message);
}
}
}
// Usage:
NotificationService service = new NotificationService();
service.subscribe(msg -> System.out.println("Received notification: " + msg));
service.notifyAll("A new task has been created");
This approach allows for easy extension of the system by adding new types of notifications and recipients without modifying existing code.