演示注册的投资者在股票市场发生变化时,可以自动得到通知。
该例子仍然使用的是传统的Observer处理手段。
UML:
首先是C#环境下的代码:
// Observer pattern -- Real World example
using System;
using System.Collections;
// "Subject"
abstract class Stock
{
// Fields
protected string symbol;
protected double price;
private ArrayList investors = new ArrayList();
// Constructor
public Stock( string symbol, double price )
{
this.symbol = symbol;
this.price = price;
}
// Methods
public void Attach( Investor investor )
{
investors.Add( investor );
}
public void Detach( Investor investor )
{
investors.Remove( investor );
}
public void Notify()
{
foreach( Investor i in investors )
i.Update( this );
}
// Properties
public double Price
{
get{ return price; }
set
{
price = value;
Notify();
}
}
public string Symbol
{
get{ return symbol; }
set{ symbol = value; }
}
}
// "ConcreteSubject"
class IBM : Stock
{
// Constructor
public IBM( string symbol, double price )
: base( symbol, price ) {}
}
// "Observer"
interface IInvestor
{
// Methods
void Update( Stock stock );
}
// "ConcreteObserver"
class Investor : IInvestor
{
// Fields
private string name;
private string observerState;
private Stock stock;
// Constructors
public Investor( string name )
{
this.name = name;
}
// Methods
public void Update( Stock stock )
{
Console.WriteLine( "Notified investor {0} of {1}'s change to {2:C}",
name, stock.Symbol, stock.Price );
}
// Properties
public Stock Stock
{
get{ return stock; }
set{ stock = value; }
}
}
/// <summary>
/// ObserverApp test
/// </summary>
public class ObserverApp
{
public static void Main( string[] args )
{
// Create investors
Investor s = new Investor( "Sorros" );
Investor b = new Investor( "Berkshire" );
// Create IBM stock and attach investors
IBM ibm = new IBM( "IBM", 120.00 );
ibm.Attach( s );
ibm.Attach( b );
// Change price, which notifies investors
ibm.Price = 120.10;
ibm.Price = 121.00;
ibm.Price = 120.50;
ibm.Price = 120.75;
}
}
Java环境下相同的代码:
//Stock
package 观察者模式;
import java.util.ArrayList;
import java.util.List;
public abstract class Stock {
protected String symbol;
protected double price;
private List<Observer> observers = new ArrayList<Observer>();
public Stock() {}
public Stock(String symbol, double price) {
this.symbol = symbol;
this.price = price;
}
public void Attach(Observer observer) {
observers.add(observer);
}
public void Detch(Observer observer) {
observers.remove(observer);
}
public void Notify() {
for(Observer o : observers){
o.update( this);;
}
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
//Observer
public interface Observer {
void update(Stock stock);
}
//IBM
public class IBM extends Stock {
public IBM() {
super();
}
public IBM(String symbol, double price) {
super(symbol, price);
}
}
//Oobserver
public class Oobserver implements Observer {
private String name;
private String observerState;
private Stock stock;
public Oobserver(String name) {
this.name = name;
}
public Stock getStock() {
return stock;
}
public void setStock(Stock stock) {
this.stock = stock;
}
@Override
public void update(Stock stock) {
System.out.print("Motified inverstor"+name+"of"+stock.symbol+"'s change to"+stock.price);
}
}
//ObserverApp
public class ObserverApp {
public static void main(String[] args) {
Oobserver s=new Oobserver("tom");
Oobserver b=new Oobserver("jary");
IBM ibm=new IBM("IBM",120.00);
ibm.Attach(s);
ibm.Attach(b);
ibm.price=120.10;
ibm.price=121.00;
ibm.price=120.50;
ibm.price=120.75;
}
}
以股民为观察着的java代码:
股民类,包含对股票的走势做出反应的方法:
class Observer {//观察者类,股民观察股票走势,所以就是股民类
private String name;
public Observer(String name) {
this.name = name;
}
public void update(Stock s){
System.out.println(s.getStockState()+" "+this.name+"已对股票"+s.getName()+"的变化做出反应!!");
}
}
股票类,每个股票可以被多个股民关注,股票变化后必须通知每个股民,所以股票和股民是一对多的聚合关系:
import java.util.ArrayList;
import java.util.List;
public class Stock {
private List<Observer> observers = new ArrayList<Observer>();
private String action;
private String name;
public Stock(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void Attach(Observer observer) {
observers.add(observer);
}
public void Detch(Observer observer) {
observers.remove(observer);
}
public void Notify() {
for(Observer o : observers){
o.update(this);
}
}
public String getStockState() {
return this.action;
}
public void setStockState(String action) {
this.action=action;
}
}
客户端测试:
public class Client {
public static void main(String[] args) {
Stock s1 = new Stock("股票一");
Stock s2 = new Stock("股票二");
Observer o1 = new Observer("张三");
Observer o2 = new Observer("李斯");
Observer o3 = new Observer("王五");
Observer o4 = new Observer("小花");
s1.setStockState("股票一大跌!!");
s1.Attach(o1);
s1.Attach(o2);
s1.Attach(o3);
s1.Notify();
s2.setStockState("股票二连涨50%!!!");
s2.Attach(o4);
s2.Notify();
}
}
输出:
股票一大跌!! 张三已对股票股票一的变化做出反应!!
股票一大跌!! 李斯已对股票股票一的变化做出反应!!
股票一大跌!! 王五已对股票股票一的变化做出反应!!
股票二连涨50%!!! 小花已对股票股票二的变化做出反应!!