Design pattern "Observer"

Let's learn what is the "Observer" design pattern πŸ’ƒπŸ‘€πŸ‘€πŸ‘€
Tuesday, July 23, 2024

Real world example

The Observer Design Pattern defines a one-to-many relationship between objects, where a change in one object (the subject) triggers updates to multiple dependent objects (observers).

In a social media context, when a user (subject) posts an update, all their followers (observers) receive notifications about the new post. This pattern helps keep the system modular and scalable.

In plain words

Defines a dependency between objects so that whenever an object changes its state, all its dependents are notified.

Wikipedia definition

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

Programmatic example

import java.util.ArrayList;
import java.util.List;
// The Subject interface
interface Subject {
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObservers();
}
// The Observer interface
interface Observer {
public void update(String status);
}
// The concrete Subject
class SocialMediaUser implements Subject {
private List<Observer> observers;
private String status;
public SocialMediaUser() {
this.observers = new ArrayList<>();
}
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(status);
}
}
public void postStatus(String status) {
this.status = status;
notifyObservers();
}
}
// The concrete Observer
class Follower implements Observer {
private String name;
public Follower(String name) {
this.name = name;
}
@Override
public void update(String status) {
System.out.println("Notification for " + name + ": Status updated to: " + status);
}
}
// The client code
public class Main {
public static void main(String[] args) {
SocialMediaUser user = new SocialMediaUser();
Follower follower1 = new Follower("Alice");
Follower follower2 = new Follower("Bob");
user.registerObserver(follower1);
user.registerObserver(follower2);
user.postStatus("Hello, world!");
}
}

Diagram

Loading graph...

Some other examples

Here are 10 real-world use cases for the Observer design pattern:

  1. Weather Station: A weather station can be observed by various smart devices. When there’s a change in weather conditions, the weather station notifies all devices about the update.
  2. Magazine Subscription: A magazine company like ReadDigest generates new content and notifies its subscribers (like a Race Car Driver, a Newspaper Man, and a Movie Director) whenever new content is available.
  3. Social Media Platforms: When a person updates their status on platforms like Facebook or Twitter, all their followers get the notification.
  4. Stock Market: Traders subscribe to a stock. When the stock price changes, all traders are notified.
  5. E-commerce Websites: When a product comes back in stock, all customers who have subscribed to notifications for that product are alerted.
  6. News Websites: When a news website publishes a new article, all subscribers receive a notification or an email.
  7. Online Auctions: Bidders subscribe to a product. When there’s a new highest bid, all bidders are notified.
  8. Job Posting Websites: Job seekers subscribe to job postings. When a new job is posted that matches their criteria, they receive a notification.
  9. Email Marketing: When a new blog post or product is launched, all email subscribers receive an email.
  10. Real-time Data Monitoring: In a real-time data monitoring system, whenever the data changes, the display elements are notified to refresh their display.

These examples illustrate how the Observer Design Pattern allows objects to communicate with each other. The subject doesn’t need to know the concrete classes of its observers, and observers can be added or removed without affecting the subject.


Recommended articles