Which design-pattern I should use in this case:
And I want to implement more technologies, and I do not want to modify the core, I want to add only modules to the system. For example, adding Telegram Messages, Twitter messages, or another email provider.
Any recommendation?
6
Answers
The case you explained is like strategy design pattern . You can use strategy design pattern and have an interface and a class for your each system that implement your interface. These are links that can help you :
tutsplus design ptterns
designpatternsphp
To me seems a PUB-SUB model or a Observer pattern is best, extension in the form of subscriber registered to publisher works well as subscriber can have their own implementation details abstracting away from core notification service.
Strategy Pattern:
Define a family of algorithms (your types of notifications), encapsulate each one (each type of notification), and make them interchangeable (with a common abstraction). Strategy lets the algorithm vary independently from the clients that use it.
Capture the abstraction in an interface, bury implementation details in derived classes.
Each time you want to add different types of notification you will add new strategies (Twitter, Telegram, ecc)
For a notification system I would suggest you using the Observer pattern. The message you receive should be inside your
Subject
. Subject should allow any number ofObservers
to attach. When a message is received, the subject should notify all the observers. ThenObservers
can read the state/message from the subject and act upon it. I am not pretty much clear about your usecase. But this would do the job.observer – observable pattern suits for you. if u use any frameworks ( spring in java) built in futures – like event listener & publisher – this really reduces ur burden of implementations.
i hope u r already using frameworks – so research on event listener + publisher. it really solves ur problem ..not only enhancing support to multiple vendors.. it also supports – single – multi thread with less changes.
According to your problem statement, two different types of design patterns will be involved:
1) Strategy Pattern: It will define the notification strategy based on the contexts like email, push, whatsapp, etc.
3) Observer Pattern: It will perform the publisher and subscribers operation will the behavior of loose coupling. It will automatically notify to subcriber.
You can also integrate RabbitMq somewhere for queuing and on time pushing messages.