How does your favorite websites like Amazon and Flipkart instantly reflect changes like when a product’s price drops and your shopping app flashes a notification, or when someone likes your post and it appears in real time on your feed?
That’s not magic — it’s the Observer Design Pattern in action.
At its core, the Observer Pattern is all about communication and synchronization. It creates a seamless link between objects — so when one object changes, all others that depend on it are automatically updated. Think of it as a digital chain reaction: one event triggers a cascade of updates, all without tightly coupling the system together.
This pattern powers everything from UI frameworks and live dashboards **to **stock market apps, chat systems, and real-time notifications. It’s the silent backbone behind modern reactive systems that need to stay always up to date.
In this post, we’ll break down how the Observer Pattern works, when to use it, and explore a real-world example to help you master one of the most elegant ways to build flexible, event-driven software.
Observable
import java.util.*;
abstract class Observable {
protected List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers(Object arg) {
for (Observer observer : observers) {
observer.update(this, arg); // push model
}
}
}
Observer
abstract class Observer {
public abstract void update(Observable observable, Object arg);
}
Product
class Product {
private String id;
private String name;
private double price;
private int stock;
public Product(String id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
public String getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
public int getStock() { return stock; }
public void setPrice(double price) { this.price = price; }
public void setStock(int stock) { this.stock = stock; }
@Override
public String toString() {
return name + " (₹" + price + ", stock: " + stock + ")";
}
}
Product Inventory
class ProductInventory extends Observable {
private Map<String, Product> products = new HashMap<>();
public void addProduct(Product product) {
products.put(product.getId(), product);
System.out.println("Added product: " + product);
notifyObservers("Added: " + product.getName());
}
public void updatePrice(String productId, double newPrice) {
Product p = products.get(productId);
if (p != null) {
p.setPrice(newPrice);
System.out.println("\nPrice updated for " + p.getName() + " -> ₹" + newPrice);
notifyObservers(p);
}
}
public void updateStock(String productId, int newStock) {
Product p = products.get(productId);
if (p != null) {
p.setStock(newStock);
System.out.println("\nStock updated for " + p.getName() + " -> " + newStock + " units");
notifyObservers(p);
}
}
public Product getProduct(String id) {
return products.get(id);
}
}
Website Observer
class WebsitePlatform extends Observer {
@Override
public void update(Observable observable, Object arg) {
if (arg instanceof Product) {
Product product = (Product) arg;
System.out.println("🌐 Website Updated: " + product.getName() +
" is now ₹" + product.getPrice() +
" | Stock: " + product.getStock());
} else if (arg instanceof String) {
System.out.println("🌐 Website Notification: " + arg);
}
}
}
Mobile App Observer
class MobileAppPlatform extends Observer {
@Override
public void update(Observable observable, Object arg) {
if (arg instanceof Product) {
Product product = (Product) arg;
System.out.println("📱 Mobile App Alert: " + product.getName() +
" now ₹" + product.getPrice() +
" | Stock: " + product.getStock());
} else if (arg instanceof String) {
System.out.println("📱 App Notification: " + arg);
}
}
}
Kiosk Observer
class KioskPlatform extends Observer {
@Override
public void update(Observable observable, Object arg) {
if (arg instanceof Product) {
Product product = (Product) arg;
System.out.println("🏪 Kiosk Screen Updated: " + product.getName() +
" — Price: ₹" + product.getPrice() +
", Stock: " + product.getStock());
}
}
}
Main Class
public class ECommerceObserverMain {
public static void main(String[] args) {
// Create observable inventory
ProductInventory inventory = new ProductInventory();
// Create observers (platforms)
WebsitePlatform website = new WebsitePlatform();
MobileAppPlatform app = new MobileAppPlatform();
KioskPlatform kiosk = new KioskPlatform();
// Register observers
inventory.addObserver(website);
inventory.addObserver(app);
inventory.addObserver(kiosk);
// Add products
Product iphone = new Product("P001", "iPhone 15", 79999, 10);
Product macbook = new Product("P002", "MacBook Air", 119999, 5);
inventory.addProduct(iphone);
inventory.addProduct(macbook);
// Simulate updates
inventory.updatePrice("P001", 75999);
inventory.updateStock("P002", 3);
}
}
Top comments (0)