ITPub博客

首页 > 应用开发 > IT综合 > Observer Design Pattern in C# and VB_NET

Observer Design Pattern in C# and VB_NET

原创 IT综合 作者:tozhangxy 时间:2006-03-13 10:28:27 0 删除 编辑

观察者设计模式(Observer Design Pattern)




适用场合

观察者模式适合的情况是对象之间是一对多的依赖,在这种情况下,如果一个对象改变了状态,它的依赖的对象被通知到,同时自动改变状态

使用的频率:

UML 类图

参与者

在模式中参与的类和对象是:

  • Subject (Stock)
    • 知道它的观察者. 所有的观察对象能观察到一个subject
    • 为添加和删除观察对象提供一个接口.
  • ConcreteSubject (IBM)
    • 储存感兴趣的subject 到ConcreteObserver
    • 当状态改变的时候,发送通知到它的观察者
  • Observer (IInvestor)
    • 为所有对象定义一个更新接口,以致subject的状态改变能被同治到.
  • ConcreteObserver (Investor)
    • 维护一个到 ConcreteSubject 对象的参考
    • 储存状态达到和subject的状态保持一致
    • 实施观察者接口来达到和subject的状态保持一致

简单的代码(c#)

下面的结构型代码论证了观察者设计模式,当状态改变的时候,对象能被通知到和改变.



// Observer pattern -- Structural example

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Observer.Structural
{

// MainApp test application

class MainApp
{
static void Main()
{
// Configure Observer pattern
ConcreteSubject s = new ConcreteSubject();

s.Attach(new ConcreteObserver(s,"X"));
s.Attach(new ConcreteObserver(s,"Y"));
s.Attach(new ConcreteObserver(s,"Z"));

// Change subject and notify observers
s.SubjectState = "ABC";
s.Notify();

// Wait for user
Console.Read();
}
}

// "Subject"

abstract class Subject
{
private ArrayList observers = new ArrayList();

public void Attach(Observer observer)
{
observers.Add(observer);
}

public void Detach(Observer observer)
{
observers.Remove(observer);
}

public void Notify()
{
foreach (Observer o in observers)
{
o.Update();
}
}
}

// "ConcreteSubject"

class ConcreteSubject : Subject
{
private string subjectState;

// Property
public string SubjectState
{
get{ return subjectState; }
set{ subjectState = value; }
}
}

// "Observer"

abstract class Observer
{
public abstract void Update();
}

// "ConcreteObserver"

class ConcreteObserver : Observer
{
private string name;
private string observerState;
private ConcreteSubject subject;

// Constructor
public ConcreteObserver(
ConcreteSubject subject, string name)
{
this.subject = subject;
this.name = name;
}

public override void Update()
{
observerState = subject.SubjectState;
Console.WriteLine("Observer {0}'s new state is {1}",
name, observerState);
}

// Property
public ConcreteSubject Subject
{
get { return subject; }
set { subject = value; }
}
}
}

Output
Observer X's new state is ABC
Observer Y's new state is ABC
Observer Z's new state is ABC



下面是一个现实世界的代码,它论证了不管股票价值何时改变,注册的对象能被通知到.



// Observer pattern -- Real World example

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Observer.RealWorld
{

// MainApp test application

class MainApp
{
static void Main()
{
// 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;

// Wait for user
Console.Read();
}
}

// "Subject"

abstract class Stock
{
protected string symbol;
protected double price;
private ArrayList investors = new ArrayList();

// Constructor
public Stock(string symbol, double price)
{
this.symbol = symbol;
this.price = price;
}

public void Attach(Investor investor)
{
investors.Add(investor);
}

public void Detach(Investor investor)
{
investors.Remove(investor);
}

public void Notify()
{
foreach (Investor investor in investors)
{
investor.Update(this);
}
Console.WriteLine("");
}

// 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
{
void Update(Stock stock);
}

// "ConcreteObserver"

class Investor : IInvestor
{
private string name;
private Stock stock;

// Constructor
public Investor(string name)
{
this.name = name;
}

public void Update(Stock stock)
{
Console.WriteLine("Notified {0} of {1}'s " +
"change to {2:C}", name, stock.Symbol, stock.Price);
}

// Property
public Stock Stock
{
get{ return stock; }
set{ stock = value; }
}
}
}

Output
Notified Sorros of IBM's change to $120.10
Notified Berkshire of IBM's change to $120.10

Notified Sorros of IBM's change to $121.00
Notified Berkshire of IBM's change to $121.00

Notified Sorros of IBM's change to $120.50
Notified Berkshire of IBM's change to $120.50

Notified Sorros of IBM's change to $120.75
Notified Berkshire of IBM's change to $120.75

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6480/viewspace-819947/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论
  • 博文量
    2
  • 访问量
    2587