Observer: sync post code blocks to repo exactly, split merged AlertObserver/PortfolioObserver block, Java 25, H6 file labels, fix stale console output, README mapping
This commit is contained in:
32
03-behavioral/observer/README.md
Normal file
32
03-behavioral/observer/README.md
Normal file
@@ -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/
|
||||||
18
README.md
18
README.md
@@ -389,6 +389,24 @@ javac 03-behavioral/memento/*.java -d out/memento
|
|||||||
java -cp out/memento memento.Main
|
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
|
## Reference
|
||||||
|
|
||||||
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
||||||
|
|||||||
Reference in New Issue
Block a user