实现观察者模式
观察者模式就两个,一个是观察者,一个是被观察者,被观察者自顾自地行动,然后告诉被观察者的观察者容器里的对象一声,说我改变了,你们看着办吧。于是,观察者收到这个消息后检查一下自己要不要做出反应,如果需要,观察者们就赶紧向上面报告,那丫的被观察者数据变了,或者发生什么事件了,或者说被观察者跳出了我们的视野了(这个?。。。。汗)。
以下是代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Mod3
{
class Program
{
static void Main(string[] args)
{
SomeKindOfUI ui = new SomeKindOfUI();
PriceMonitor pm = new PriceMonitor();
SomeData data = new SomeData();
data.Register(ui);
data.Register(pm);
data.Price = 1.14f;
data.Price = 1.5f;
data.Price = 2.0f;
data.Price = 3.19f;
data.UnRegister(ui);
Console.Read();
}
}
public interface IObserver
{
void Notify(object anobject);
}
public interface IObservable
{
void Register(IObserver anObject);
void UnRegister(IObserver anobject);
}
public class ObservableImpl : IObservable
{
protected Hashtable _observerContainer = new Hashtable();
public void Register(IObserver anObject)
{
_observerContainer.Add(anObject, anObject);
}
public void UnRegister(IObserver anObject)
{
_observerContainer.Remove(anObject);
}
public void NotifyObservers(object anObject)
{
foreach (IObserver anObserver in _observerContainer.Keys)
{
anObserver.Notify(anObject);
}
}
}
public class SomeData : ObservableImpl
{
float m_price;
public float Price
{
set
{
m_price = value;
base.NotifyObservers(m_price);
}
}
}
public class SomeKindOfUI : IObserver
{
public void Notify(object anObject)
{
Console.WriteLine("the Price Newest value is" + anObject.ToString());
}
}
public class PriceMonitor: IObserver
{
public void Notify(object anObject)
{
if ( Convert.ToDouble( anObject) >3 )
{
Console.Write("I am a PriceMonitor, Your Price is"+anObject.ToString()+",And It is too High!\n");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Mod3
{
class Program
{
static void Main(string[] args)
{
SomeKindOfUI ui = new SomeKindOfUI();
PriceMonitor pm = new PriceMonitor();
SomeData data = new SomeData();
data.Register(ui);
data.Register(pm);
data.Price = 1.14f;
data.Price = 1.5f;
data.Price = 2.0f;
data.Price = 3.19f;
data.UnRegister(ui);
Console.Read();
}
}
public interface IObserver
{
void Notify(object anobject);
}
public interface IObservable
{
void Register(IObserver anObject);
void UnRegister(IObserver anobject);
}
public class ObservableImpl : IObservable
{
protected Hashtable _observerContainer = new Hashtable();
public void Register(IObserver anObject)
{
_observerContainer.Add(anObject, anObject);
}
public void UnRegister(IObserver anObject)
{
_observerContainer.Remove(anObject);
}
public void NotifyObservers(object anObject)
{
foreach (IObserver anObserver in _observerContainer.Keys)
{
anObserver.Notify(anObject);
}
}
}
public class SomeData : ObservableImpl
{
float m_price;
public float Price
{
set
{
m_price = value;
base.NotifyObservers(m_price);
}
}
}
public class SomeKindOfUI : IObserver
{
public void Notify(object anObject)
{
Console.WriteLine("the Price Newest value is" + anObject.ToString());
}
}
public class PriceMonitor: IObserver
{
public void Notify(object anObject)
{
if ( Convert.ToDouble( anObject) >3 )
{
Console.Write("I am a PriceMonitor, Your Price is"+anObject.ToString()+",And It is too High!\n");
}
}
}
}
实现了一个被观察者 SomeData ,实现了两个观察者,一个是 SomeKindUI 的观象,一个是 PriceMonitor 的对象。如果 SomeData 发生了改变,会通知容器里的对象,ui和pm就都会做出反应。