奔跑的肥猪

导航

Observer pattern

定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够相应的发生变化.

那华尔街的例子举个例子:

投资者关注股票,但他没有必要整天在证交所目不转睛的关注股票的涨落,而是应该如果股票有变化,告诉投资者就可以了。

do not call me, i will call you.

public class IBMStock extends Stock {

 

}

public interface IInvestor {
void Update( Stock stock );
}
 
public class ShanghaiInvestor implements IInvestor {
 
private String name;
 
public ShanghaiInvestor(String name) {
super();
this.name = name;
}
 
@Override
public void Update(Stock stock) {
DebugLog.log("the investor" + this.name + "stock has change"
+ stock.getName() + "the price is" + stock.getPrice());
}
 
}
 
 
public abstract class Stock {
protected String name;
protected double price;
private List<IInvestor> inVestors = new ArrayList<IInvestor>();
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public double getPrice() {
return price;
}
 
public void setPrice(double price) {
this.price = price;
Notify();
}
 
public void attach(IInvestor investor) {
inVestors.add(investor);
}
 
public void detach(IInvestor investor) {
inVestors.remove(investor);
}
 
public void Notify()
{
int size = inVestors.size();
for(int start=0;start<size;start++)
{
IInvestor investor=inVestors.get(start);
investor.Update(this);
}
}
 
}
 
 
public class WallStreeInvestor implements IInvestor {
 
private String name;
 
public WallStreeInvestor(String name) {
super();
this.name = name;
}
 
@Override
public void Update(Stock stock) {
DebugLog.log("the investor" + this.name + "stock has change"
+ stock.getName() + "the price is" + stock.getPrice());
}
 
}
 
 
public class Test {
 
/**
* @param args
*/
public static void main(String[] args) {
Stock aStock=new IBMStock();
aStock.setName("IBM stock");
IInvestor sInvestor=new ShanghaiInvestor("Shanghai");
IInvestor wInvestor=new WallStreeInvestor("WallStreet");
aStock.attach(sInvestor);
aStock.attach(wInvestor);
aStock.setPrice(1000);
DebugLog.printLogSeperator();
aStock.detach(wInvestor);
aStock.setPrice(2000);
DebugLog.printLogSeperator();
aStock.detach(sInvestor);
aStock.setPrice(5000);
DebugLog.printLogSeperator();
}
 
}
 
从代码中看出一个问题,Stock类中为什么要加入attach & detach方法啊,
个人觉得应该加入一个manager 控制stock的添加和减少
 
public class HPStock extends Stock {
 
public HPStock(String name, double price) {
super(name, price);
}
 
}
 
 
public class IBMStock extends Stock {
 
public IBMStock(String name, double price) {
super(name, price);
}
 
}
 
 
public interface IInvestor {
void Update( Stock stock );
}
 
 
public interface IManager {
public void attach(IInvestor investor);
public void detach(IInvestor investor);
public void Notify(Stock stock);
}
 
 
public class ShangHaiInvestor implements IInvestor {
 
private String name;
 
public ShangHaiInvestor(String name) {
super();
this.name = name;
}
 
@Override
public void Update(Stock stock) {
DebugLog.log("the investor" + this.name + "stock has change"
+ stock.getName() + "the price is" + stock.getPrice());
}
 
}
 
 
public abstract class Stock {
public Stock(String name, double price) {
super();
this.name = name;
this.price = price;
}
 
protected String name;
protected double price;
protected IManager imanager;
public IManager getImanager() {
return imanager;
}
 
public void setImanager(IManager imanager) {
this.imanager = imanager;
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public double getPrice() {
return price;
}
 
public void setPrice(double price) {
this.price = price;
imanager.Notify(this);
}
 
}
 
 
public class StockManager implements IManager {
private List<IInvestor> inVestors = new ArrayList<IInvestor>();
public void attach(IInvestor investor) {
inVestors.add(investor);
}
 
public void detach(IInvestor investor) {
inVestors.remove(investor);
}
 
public void Notify(Stock stock)
{
int size = inVestors.size();
for(int start=0;start<size;start++)
{
IInvestor investor=inVestors.get(start);
investor.Update(stock);
}
}
}
 
public class WallStreeInvestor implements IInvestor {
 
private String name;
 
public WallStreeInvestor(String name) {
super();
this.name = name;
}
 
@Override
public void Update(Stock stock) {
DebugLog.log("the investor" + this.name + "stock has change"
+ stock.getName() + "the price is" + stock.getPrice());
}
 
}
 
 
public class Test {
 
/**
* @param args
*/
public static void main(String[] args) {
Stock iStock=new IBMStock("IBM stock",1000);
Stock hStock=new HPStock("HP stock",1000);
 
IInvestor sInvestor=new ShangHaiInvestor("Shanghai");
IInvestor wInvestor=new WallStreeInvestor("WallStreet");
IManager iManager=new StockManager();
iManager.attach(sInvestor);
iManager.attach(wInvestor);
 
iStock.setImanager(iManager);
hStock.setImanager(iManager);
 
iStock.setPrice(1000);
iStock.setPrice(2000);
iStock.setPrice(5000);
hStock.setPrice(5000);
}
 
}
 

posted on 2011-12-23 18:15  布兜兜  阅读(142)  评论(0编辑  收藏  举报