設計模式——觀察者

觀察者模式定義了對象之間的一對多依賴,當一個對象改變狀態時,他的全部的依賴者都會收到通知並自動更新;
定義主題,主題的功能是持有觀察者的引用,提供註冊、取消註冊和通知,而且有狀態;觀察者實現接口中的update方法,必須註冊具體主題;
 
JDK自帶了對觀察者的支持功能
被觀察者Observable對象,提供add/delete/notifyObserver()和setChanged()設置狀態方法
觀察者Observer接口,提供update()方法進行相關的更新操做。
 
觀察者類圖:
 
 
如下程序模擬股票的狀態更新功能(使用jdk對觀察者支持,參考網上實現)
 
1.定義Observable 接口實現類,該類是設置股票的信息,而且在數據更新後會通知全部的觀察者進行相關狀態的更新。
 
package com.observer;

import java.util.Observable;

public class StockData extends Observable {
	private String symbol;
	private float close;
	private float high;
	private float low;
	private long volume;

	public StockData() {
	}

	public String getSymbol() {
		return symbol;
	}

	public float getClose() {
		return close;
	}

	public float getHigh() {
		return high;
	}

	public float getLow() {
		return low;
	}

	public long getVolume() {
		return volume;
	}

	public void sendStockData() {
		setChanged();
		notifyObservers();
	}

	public void setStockData(String symbol, float close, float high, float low,
			long volume) {
		this.symbol = symbol;
		this.close = close;
		this.high = high;
		this.low = low;
		this.volume = volume;
		sendStockData();
	}
}

 

2.實現觀察者Observer實現類,模擬股票的大買家java

package com.observer;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Observable;
import java.util.Observer;

public class BigBuyer implements Observer {
	private String symbol;
	private float close;
	private float high;
	private float low;
	private long volume;

	public BigBuyer(Observable observable) {
		observable.addObserver(this); // 註冊關係
	}

	public void update(Observable observable, Object args) {
		if (observable instanceof StockData) {
			StockData stockData = (StockData) observable;
			this.symbol = stockData.getSymbol();
			this.close = stockData.getClose();
			this.high = stockData.getHigh();
			this.low = stockData.getLow();
			this.volume = stockData.getVolume();
			display();
		}
	}

	public void display() {
		DecimalFormatSymbols dfs = new DecimalFormatSymbols();
		DecimalFormat volumeFormat = new DecimalFormat("###,###,###,###", dfs);
		DecimalFormat priceFormat = new DecimalFormat("###.00", dfs);
		System.out.println("Big Buyer reports... ");
		System.out.println("\tThe lastest stock quote for " + symbol + " is:");
		System.out.println("\t$" + priceFormat.format(close)
				+ " per share (close).");
		System.out.println("\t$" + priceFormat.format(high)
				+ " per share (high).");
		System.out.println("\t$" + priceFormat.format(low)
				+ " per share (low).");
		System.out.println("\t" + volumeFormat.format(volume)
				+ " shares traded.");
		System.out.println();
	}
}

3.實現觀察者Observer實現類,做爲第二個觀察者測試

 

package com.observer;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Observable;
import java.util.Observer;

public class TradingFool implements Observer
{
private String symbol;
private float close;

public TradingFool(Observable observable)
	{
	observable.addObserver(this);//註冊關係
	}

public void update(Observable observable,Object args)
	{
	if(observable instanceof StockData)
		{
		StockData stockData = (StockData)observable;
		this.symbol = stockData.getSymbol();
		this.close = stockData.getClose();
		display();
		}
	}

public void display()
	{
	DecimalFormatSymbols dfs = new DecimalFormatSymbols();
	DecimalFormat priceFormat = new DecimalFormat("###.00",dfs);
	System.out.println("Trading Fool says... ");
	System.out.println("\t" + symbol + " is currently trading at $" + priceFormat.format(close) + " per share.");
	System.out.println();
	}
}

  

4.測試this

package com.observer;
public class StockQuotes
{
public static void main(String[] args)
	{
	System.out.println();
	System.out.println("-- Stock Quote Application --");
	System.out.println();

	StockData stockData = new StockData();

	new TradingFool(stockData);
	new BigBuyer(stockData);

	stockData.setStockData("JUPM",16.10f,16.15f,15.34f,(long)481172);
	stockData.setStockData("SUNW",4.84f,4.90f,4.79f,(long)68870233);
	stockData.setStockData("MSFT",23.17f,23.37f,23.05f,(long)75091400);
	}
}
相關文章
相關標籤/搜索