Last active
November 30, 2023 09:33
-
-
Save kishankg/ada59bfbca38cf30e6966ab4b748a517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
// Observer interface | |
interface Observer { | |
void update(String weather); | |
} | |
// Subject interface | |
interface Subject { | |
void addObserver(Observer observer); | |
void removeObserver(Observer observer); | |
void notifyObservers(); | |
} | |
// Concrete Subject | |
class WeatherStation implements Subject { | |
private List<Observer> observers = new ArrayList<>(); | |
private String currentWeather; | |
public void setWeather(String weather) { | |
this.currentWeather = weather; | |
notifyObservers(); | |
} | |
@Override | |
public void addObserver(Observer observer) { | |
observers.add(observer); | |
} | |
@Override | |
public void removeObserver(Observer observer) { | |
observers.remove(observer); | |
} | |
@Override | |
public void notifyObservers() { | |
for (Observer observer : observers) { | |
observer.update(currentWeather); | |
} | |
} | |
} | |
// Concrete Observer | |
class DisplayDevice implements Observer { | |
private String deviceName; | |
public DisplayDevice(String deviceName) { | |
this.deviceName = deviceName; | |
} | |
@Override | |
public void update(String weather) { | |
System.out.println("Display device " + deviceName + " updated with weather: " + weather); | |
} | |
} | |
public class WeatherStationExample { | |
public static void main(String[] args) { | |
// Creating a weather station | |
WeatherStation weatherStation = new WeatherStation(); | |
// Creating display devices (observers) | |
DisplayDevice phoneDisplay = new DisplayDevice("Phone"); | |
DisplayDevice tvDisplay = new DisplayDevice("TV"); | |
// Registering observers with the subject (weather station) | |
weatherStation.addObserver(phoneDisplay); | |
weatherStation.addObserver(tvDisplay); | |
// Simulating a change in weather | |
weatherStation.setWeather("Sunny"); | |
// Unregistering an observer | |
weatherStation.removeObserver(tvDisplay); | |
// Simulating another change in weather | |
weatherStation.setWeather("Rainy"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
// Observer interface | |
interface Observer { | |
void update(); | |
} | |
// Subject interface | |
interface Subject { | |
void addObserver(Observer observer); | |
void removeObserver(Observer observer); | |
void notifyObservers(); | |
} | |
// Concrete Subject | |
class StockMarket implements Subject { | |
private List<Observer> observers = new ArrayList<>(); | |
private String stockSymbol; | |
private double stockPrice; | |
public StockMarket(String stockSymbol, double initialPrice) { | |
this.stockSymbol = stockSymbol; | |
this.stockPrice = initialPrice; | |
} | |
public void simulateMarketChanges() { | |
// Simulate changes in stock prices | |
Random random = new Random(); | |
double percentageChange = (random.nextDouble() - 0.5) * 5; // +/- 2.5% | |
stockPrice += (stockPrice * percentageChange) / 100; | |
notifyObservers(); | |
} | |
@Override | |
public void addObserver(Observer observer) { | |
observers.add(observer); | |
} | |
@Override | |
public void removeObserver(Observer observer) { | |
observers.remove(observer); | |
} | |
@Override | |
public void notifyObservers() { | |
for (Observer observer : observers) { | |
observer.update(); | |
} | |
} | |
public String getStockSymbol() { | |
return stockSymbol; | |
} | |
public double getStockPrice() { | |
return stockPrice; | |
} | |
} | |
// Concrete Observer: Financial Institution | |
class FinancialInstitution implements Observer { | |
private String institutionName; | |
private StockMarket stockMarket; | |
public FinancialInstitution(String institutionName, StockMarket stockMarket) { | |
this.institutionName = institutionName; | |
this.stockMarket = stockMarket; | |
} | |
@Override | |
public void update() { | |
System.out.println("Financial Institution " + institutionName + | |
" received update for stock " + stockMarket.getStockSymbol() + ": $" + stockMarket.getStockPrice()); | |
// Implement custom strategy for responding to stock changes (e.g., buy/sell decisions) | |
} | |
} | |
// Concrete Observer: Individual Investor | |
class IndividualInvestor implements Observer { | |
private String investorName; | |
private StockMarket stockMarket; | |
public IndividualInvestor(String investorName, StockMarket stockMarket) { | |
this.investorName = investorName; | |
this.stockMarket = stockMarket; | |
} | |
@Override | |
public void update() { | |
System.out.println("Individual Investor " + investorName + | |
" received update for stock " + stockMarket.getStockSymbol() + ": $" + stockMarket.getStockPrice()); | |
// Implement custom strategy for responding to stock changes (e.g., portfolio adjustments) | |
} | |
} | |
public class StockMarketExample { | |
public static void main(String[] args) { | |
// Creating a stock market with an initial stock | |
StockMarket stockMarket = new StockMarket("XYZ", 100.0); | |
// Creating observers (financial institutions and individual investors) | |
FinancialInstitution institution1 = new FinancialInstitution("Bank A", stockMarket); | |
FinancialInstitution institution2 = new FinancialInstitution("Investment Firm B", stockMarket); | |
IndividualInvestor investor1 = new IndividualInvestor("John Doe", stockMarket); | |
IndividualInvestor investor2 = new IndividualInvestor("Jane Smith", stockMarket); | |
// Registering observers with the subject (stock market) | |
stockMarket.addObserver(institution1); | |
stockMarket.addObserver(institution2); | |
stockMarket.addObserver(investor1); | |
stockMarket.addObserver(investor2); | |
// Simulating changes in the stock market | |
for (int i = 0; i < 5; i++) { | |
stockMarket.simulateMarketChanges(); | |
System.out.println("-----------------------------"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment