标签:
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实现类,模拟股票的大买家
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.测试
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); } }
标签:
原文地址:http://www.cnblogs.com/lcqBlogs/p/4278205.html