From 03c93b503718bceb73c223a5810a263c62744b2f Mon Sep 17 00:00:00 2001 From: Ankur Date: Wed, 24 Jun 2026 16:52:38 +0530 Subject: [PATCH] Observer: sync post code blocks to repo exactly, split merged AlertObserver/PortfolioObserver block, Java 25, H6 file labels, fix stale console output, README mapping --- 03-behavioral/observer/README.md | 32 ++++++++++++++++++++++++++++++++ README.md | 18 ++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 03-behavioral/observer/README.md diff --git a/03-behavioral/observer/README.md b/03-behavioral/observer/README.md new file mode 100644 index 0000000..d354296 --- /dev/null +++ b/03-behavioral/observer/README.md @@ -0,0 +1,32 @@ +# Observer Design Pattern — Java Example + +**Pattern:** Behavioral → Observer +**Article:** https://ankurm.com/observer-design-pattern-java/ + +## What this example shows + +A stock market subject that notifies subscribers without knowing what they do with the notification. `StockObserver` is the one-method interface every subscriber implements. `StockMarket` is the subject: it holds the observer list and pushes both the old and new price to every subscriber whenever `setPrice()` is called. `AlertObserver` and `PortfolioObserver` are two concrete observers that react completely differently to the same notification — one watches for threshold breaches, the other computes a P&L delta. `Main` subscribes all three, moves the price twice, then unsubscribes Bob before a third move so his portfolio observer never sees it. + +## How to run + +```bash +javac observer/*.java -d out/observer +java -cp out/observer observer.Main +``` + +Requires Java 25. + +## Post Section ↔ File Mapping + +| Post Section | File(s) | +|---|---| +| Implementation: Stock Price Monitoring — the Observer interface | `StockObserver.java` | +| Implementation: Stock Price Monitoring — the Subject | `StockMarket.java` | +| Implementation: Stock Price Monitoring — AlertObserver | `AlertObserver.java` | +| Implementation: Stock Price Monitoring — PortfolioObserver | `PortfolioObserver.java` | +| Implementation: Stock Price Monitoring — wiring it together | `Main.java` | + +Note: the "Push vs Pull Notification Models" pull-model snippet (`PullAlertObserver`) and the "Thread Safety" `CopyOnWriteArrayList` snippet are illustrative only — they are not part of this repository's runnable example. + +Article: https://ankurm.com/observer-design-pattern-java/ +All patterns: https://ankurm.com/design-patterns-java/ diff --git a/README.md b/README.md index 59f67ad..1ccb34b 100644 --- a/README.md +++ b/README.md @@ -389,6 +389,24 @@ javac 03-behavioral/memento/*.java -d out/memento java -cp out/memento memento.Main ``` +### Observer (`03-behavioral/observer/`) + +| Post Section | File(s) | +|---|---| +| Implementation: Stock Price Monitoring — the Observer interface | `StockObserver.java` | +| Implementation: Stock Price Monitoring — the Subject | `StockMarket.java` | +| Implementation: Stock Price Monitoring — AlertObserver | `AlertObserver.java` | +| Implementation: Stock Price Monitoring — PortfolioObserver | `PortfolioObserver.java` | +| Implementation: Stock Price Monitoring — wiring it together | `Main.java` | + +Note: the "Push vs Pull Notification Models" pull-model snippet and the "Thread Safety" `CopyOnWriteArrayList` snippet are illustrative only — they are not part of this repository's runnable example. + +Run it: +```bash +javac 03-behavioral/observer/*.java -d out/observer +java -cp out/observer observer.Main +``` + ## Reference - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides