Add all 23 GoF design pattern implementations

This commit is contained in:
2026-07-25 10:50:29 +05:30
commit f5688a6b32
164 changed files with 4371 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 ===");
}
}