The Observer Design Pattern is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object (the subject) changes state, all its dependents (observers) are notified and updated automatically.
It primarily deals with the interaction and communication between objects, specifically focusing on how objects behave in response to changes in the state of other objects.
· The pattern is concerned with defining a mechanism for a group of objects to interact based on changes in the state of one object (the subject). The observers’ behavior is triggered by changes in the subject’s state.
· It encapsulates the behavior of the dependent objects (observers) and allows for a clean separation between the subject and its observers. This separation promotes a more modular and maintainable design.
· The pattern promotes loose coupling between the subject and its observers. The subject doesn’t need to know the concrete classes of its observers, and observers can be added or removed without affecting the subject.
· The primary mechanism in the Observer Pattern is the notification of observers when a change occurs. This notification mechanism facilitates the dynamic and coordinated behavior of multiple objects in response to changes in the subject.
enum
Event
{
NEW_ITEM,
SALE
}
interface
Listener
{
void
update(Event eventType);
}
record
EmailMsgListener(String email) implements Listener {
@Override
public void update(Event eventType) {
// Actually send the mail
System.out.println("Sending mail to " + email + " concerning " + eventType);
}
}
record
MobileAppListener(String username) implements Listener {
@Override
public void update(Event eventType) {
// Actually send the push notification to username
System.out.println("Sending mobile app notification to " + username + " concerning
" +
eventType);
}
}
class
NotificationService
{
private
final Map<Event, List<Listener>> customers;
public
NotificationService() {
customers
= new HashMap<>();
Arrays.stream(Event.values()).forEach(event -> customers.put(event, new ArrayList<>()));
}
public
void subscribe(Event eventType, Listener listener) {
customers.get(eventType).add(listener);
}
public
void unsubscribe(Event eventType, Listener listener) {
customers.get(eventType).remove(listener);
}
public
void notifyCustomers(Event eventType) {
customers.get(eventType).forEach(listener -> listener.update(eventType));
}
}
class
Store
{
private
final NotificationService
notificationService;
public
Store() {
notificationService
= new NotificationService();
}
public
void newItemPromotion() {
notificationService.notifyCustomers(Event.NEW_ITEM);
}
public
void salePromotion() {
notificationService.notifyCustomers(Event.SALE);
}
public
NotificationService
getService() {
return notificationService;
}
}
public
class ObserverPattern
{
public
static void main(String[] args) {
Store
store = new Store();
store.getService().subscribe(Event.NEW_ITEM, new EmailMsgListener("geekific@like.com"));
store.getService().subscribe(Event.SALE, new EmailMsgListener("geekific@like.com"));
store.getService().subscribe(Event.SALE, new EmailMsgListener("geekific@subs.com"));
store.getService().subscribe(Event.NEW_ITEM, new MobileAppListener("GeekificLnS"));
store.newItemPromotion();
System.out.println("==========================================");
store.salePromotion();
System.out.println("==========================================");
store.getService().unsubscribe(Event.SALE, new EmailMsgListener("geekific@like.com"));
store.salePromotion();
}
}
Sending mail to geekific@like.com concerning NEW_ITEM
Sending mobile app notification to GeekificLnS concerning NEW_ITEM
==========================================
Sending mail to geekific@like.com concerning SALE
Sending mail to geekific@subs.com concerning SALE
==========================================
Sending mail to geekific@subs.com concerning SALE