Add all 23 GoF design pattern implementations (2026-06-13)

This commit is contained in:
Ankur
2026-06-13 21:44:56 +05:30
commit a5beb61425
106 changed files with 2977 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package observer;
/**
* Observer Design Pattern — Runnable Demo
* Run: javac observer/*.java -d out/observer && java -cp out/observer observer.Main
* Article: https://ankurm.com/observer-design-pattern-java/
*/
public class Main {
public static void main(String[] args) {
System.out.println("=== Observer Design Pattern Demo ===\n");
StockMarket aapl = new StockMarket("AAPL", 175.00);
StockObserver alert = new AlertObserver("RiskEngine", 2.0);
StockObserver alice = new PortfolioObserver("Alice", 100);
StockObserver bob = new PortfolioObserver("Bob", 50);
aapl.subscribe(alert);
aapl.subscribe(alice);
aapl.subscribe(bob);
System.out.println();
aapl.setPrice(178.50); // +2% — should trigger alert
System.out.println();
aapl.setPrice(179.00); // small move
System.out.println("\n-- Bob unsubscribes --");
aapl.unsubscribe(bob);
System.out.println();
aapl.setPrice(165.00); // big drop — Bob doesn't hear it
System.out.println("\n=== Demo complete ===");
}
}