1、观察者模式定义:
定义对象之间的一对多依赖,如此,一个对象的状态改变时,他的所有依赖者都会收到通知,并自动更新;
2、关键点
该模式定义了两个对象之间的松耦合关系,观察者不知被观察者的细节,被观察者不必知道观察者的具体表现,只要双方交互的接口未变,在扩展性的观察者时,被观察者不必修改代码;
3、例子源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObserverPattern
{
public interface Observer
{
void update(string tem,string hum,string pre);
}
public interface DisplayElement
{
void display();
}
public interface Subject
{
void registerObserver(Observer observer);
void removeRegister(Observer observer);
void notifyObservers();
}
public class WeatherData : Subject
{
public WeatherData()
{
vecObservers = new List<Observer>();
}
#region Subject 成员
public void registerObserver(Observer observer)
{
vecObservers.Add(observer);
}
public void removeRegister(Observer observer)
{
vecObservers.Remove(observer);
}
public void notifyObservers()
{
foreach (Observer observer in vecObservers)
{
observer.update(m_tem, m_hum, m_pre);
}
}
#endregion
public void SetMeasureMent(string tem, string hum, string pre)
{
m_tem = tem;
m_hum = hum;
m_pre = pre;
notifyObservers();
}
private string m_tem;
private string m_hum;
private string m_pre;
private List<Observer> vecObservers;
}
public class CurrentConditionDisplay : Observer, DisplayElement
{
public CurrentConditionDisplay(Subject pSubject)
{
m_Subject = pSubject;
m_Subject.registerObserver(this);
}
#region Observer 成员
public void update(string tem, string hum, string pre)
{
m_tem = tem;
m_hum = hum;
m_pre = pre;
display();
}
#endregion
#region DisplayElement 成员
public void display()
{
Console.WriteLine(string.Format("当前显示牌显示状态,{0},{1},{2}", m_tem, m_hum, m_pre));
}
#endregion
private string m_tem;
private string m_hum;
private string m_pre;
private Subject m_Subject;
}
public class StatisticsConditionDisplay : Observer, DisplayElement
{
public StatisticsConditionDisplay(Subject pSubject)
{
m_Subject = pSubject;
m_Subject.registerObserver(this);
}
#region Observer 成员
public void update(string tem, string hum, string pre)
{
m_tem = tem;
m_hum = hum;
m_pre = pre;
display();
}
#endregion
#region DisplayElement 成员
public void display()
{
Console.WriteLine(string.Format("统计显示牌显示状态,{0},{1},{2}", m_tem, m_hum, m_pre));
}
#endregion
private string m_tem;
private string m_hum;
private string m_pre;
private Subject m_Subject;
}
public class ForecastConditionDisplay : Observer, DisplayElement
{
public ForecastConditionDisplay(Subject pSubject)
{
m_Subject = pSubject;
m_Subject.registerObserver(this);
}
#region Observer 成员
public void update(string tem, string hum, string pre)
{
m_tem = tem;
m_hum = hum;
m_pre = pre;
display();
}
#endregion
#region DisplayElement 成员
public void display()
{
Console.WriteLine(string.Format("预报显示牌显示状态,{0},{1},{2}", m_tem, m_hum, m_pre));
}
#endregion
private string m_tem;
private string m_hum;
private string m_pre;
private Subject m_Subject;
}
}
namespace ObserverPattern
{
class Program
{
static void Main(string[] args)
{
WeatherData pSubject = new WeatherData();
Observer pObserver1 = new CurrentConditionDisplay(pSubject);
Observer pObserver2 = new StatisticsConditionDisplay(pSubject);
Observer pObserver3 = new ForecastConditionDisplay(pSubject);
pSubject.SetMeasureMent("100", "100", "100");
pSubject.SetMeasureMent("100", "200", "300");
pSubject.SetMeasureMent("300", "200", "100");
pSubject.removeRegister(pObserver3);
pSubject.SetMeasureMent("1000", "1000", "1000");
}
}
}