觀察者模式用於描述物件之間的依賴關係,它引入了觀察者和觀察目標兩類不同的角色,由於提供了抽象層,它使得增加新的觀察者和觀察目標都很方便。觀察者模式廣泛應用於各種程式語言的事件處理模型中,Java語言也提供了對觀察者模式的全面支援。
抽象目標類
public abstract class Subject {
protected ArrayList observers<Observer> = new ArrayList();
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public abstract void notify( );
}
具體目標類
public class ConcreteSubject extends Subject {
//實現通知方法
public void notify() {
for(Object obs:observers) {
((Observer)obs).update();
}
}
}
抽象觀察者類
public interface Observer {
public void update();
}
具體觀察者類
public class ConcreteObserver implements Observer {
public void update() {
……
}
}
股票
抽象目標類:股票
public interface Stocks {
ArrayList<Observer> OBSERVERS = new ArrayList<>();
void attach(Observer observer);
void detach(Observer observer);
void notifyInvestor();
}
目標類:股票
public class Stock implements Stocks{
private String stockName;
private int price;
public Stock(String stockName, int price) {
this.stockName = stockName;
this.price = price;
}
public Stock() {
}
public String getStockName() {
return stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public void attach(Observer observer) {
OBSERVERS.add(observer);
}
@Override
public void detach(Observer observer) {
OBSERVERS.remove(observer);
}
@Override
public void notifyInvestor() {
if(price >= 25*1.05 || price <= 25*0.95){
System.out.println("通知:股票價格變動幅度超過5%!");
for (Observer observer: OBSERVERS) {
observer.upDate();
}
} else {
System.out.println("股票價格變化幅度沒有超過5%!!");
}
}
}
抽象觀察者類
public interface Observer {
void upDate();
}
具體觀察者類A
public class ConcreteObserverA implements Observer {
private String name;
private Stock stock;
public ConcreteObserverA(String name, Stock stock) {
this.name = name;
this.stock = stock;
}
public ConcreteObserverA(String name) {
this.name = name;
}
@Override
public void upDate() {
System.out.println("通知"+name+":"+stock.getStockName()+"的股票價格變化幅度超過5%,股票價格為:"+stock.getPrice());
}
}
具體觀察者類B
public class ConcreteObserverB implements Observer {
private String name;
public ConcreteObserverB(String name, Stock stock) {
this.name = name;
this.stock = stock;
}
public ConcreteObserverB(String name) {
this.name = name;
}
private Stock stock;
@Override
public void upDate() {
System.out.println("通知"+name+":"+stock.getStockName()+"的股票價格變化幅度超過5%,股票價格為:"+stock.getPrice());
}
}
具體觀察者類C
public class ConcreteObserverC implements Observer {
private String name;
private Stock stock;
public ConcreteObserverC(String name, Stock stock) {
this.name = name;
this.stock = stock;
}
public ConcreteObserverC(String name) {
this.name = name;
}
@Override
public void upDate() {
System.out.println("通知"+name+":"+stock.getStockName()+"的股票價格變化幅度超過5%,股票價格為:"+stock.getPrice());
}
}
測試類
public class Test {
public static void main(String[] args) {
System.out.println("設計模式,2020006924,於鑫");
Stock stock1 = new Stock("股票1",25);
System.out.println(stock1.getStockName()+"的股價為:"+stock1.getPrice());
Observer obs1,obs2,obs3;
obs1 = new ConcreteObserverA("smith",stock1);
obs2 = new ConcreteObserverB("tom",stock1);
obs3 = new ConcreteObserverC("李白",stock1);
stock1.attach(obs1);
stock1.attach(obs2);
stock1.attach(obs3);
stock1.setPrice(22);
stock1.notifyInvestor();
}
}
本文來自部落格園,作者:街酒,轉載請註明原文連結:https://www.cnblogs.com/sorrymine/p/17421523.html