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,22 @@
package observer;
/** Fires an alert when the price changes by more than a threshold % */
public class AlertObserver implements StockObserver {
private final String name;
private final double thresholdPercent;
public AlertObserver(String name, double thresholdPercent) {
this.name = name;
this.thresholdPercent = thresholdPercent;
}
@Override
public void onPriceChanged(String ticker, double oldPrice, double newPrice) {
double change = Math.abs((newPrice - oldPrice) / oldPrice * 100);
if (change >= thresholdPercent) {
System.out.printf(" [ALERT:%s] %s moved %.1f%% — ALERT TRIGGERED%n", name, ticker, change);
} else {
System.out.printf(" [Alert:%s] %s moved %.1f%% — within threshold%n", name, ticker, change);
}
}
}

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 ===");
}
}

View File

@@ -0,0 +1,19 @@
package observer;
/** Updates portfolio value whenever a held stock changes price */
public class PortfolioObserver implements StockObserver {
private final String ownerName;
private final int sharesHeld;
public PortfolioObserver(String ownerName, int sharesHeld) {
this.ownerName = ownerName;
this.sharesHeld = sharesHeld;
}
@Override
public void onPriceChanged(String ticker, double oldPrice, double newPrice) {
double gain = (newPrice - oldPrice) * sharesHeld;
System.out.printf(" [Portfolio:%s] %s×%d P&L change: %+.2f%n",
ownerName, ticker, sharesHeld, gain);
}
}

View File

@@ -0,0 +1,36 @@
package observer;
import java.util.ArrayList;
import java.util.List;
/**
* Subject (Observable) — maintains a list of observers and notifies them
* when the stock price changes. Observers register and deregister freely.
*/
public class StockMarket {
private final String ticker;
private double price;
private final List<StockObserver> observers = new ArrayList<>();
public StockMarket(String ticker, double initialPrice) {
this.ticker = ticker;
this.price = initialPrice;
System.out.println("[Market] " + ticker + " initialised at $" + initialPrice);
}
public void subscribe(StockObserver observer) { observers.add(observer); }
public void unsubscribe(StockObserver observer) { observers.remove(observer); }
public void setPrice(double newPrice) {
double old = this.price;
this.price = newPrice;
System.out.printf("[Market] %s price: $%.2f -> $%.2f%n", ticker, old, newPrice);
notifyObservers(old, newPrice);
}
private void notifyObservers(double old, double newPrice) {
for (StockObserver o : observers) {
o.onPriceChanged(ticker, old, newPrice);
}
}
}

View File

@@ -0,0 +1,6 @@
package observer;
/** Observer interface — all subscribers implement this */
public interface StockObserver {
void onPriceChanged(String ticker, double oldPrice, double newPrice);
}