Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
19
03-behavioral/observer/PortfolioObserver.java
Normal file
19
03-behavioral/observer/PortfolioObserver.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user