一、原始模式
1. 定义一个主题接口类 【ISubject】,所有的被观察都要实现该接口
public interface ISubject{void RegisterObserver(IObserver o); //注册观察者void RemoveObserver(IObserver o); //注销观察者void notifyObservers(); // 数据发生变化时,通知观察者}
- 定义一个观察者接口 【IObserver】,所有的观察者都需要实现该接口的 Update 方法 ```csharp interface IObserver { void Update(float temp,float humidity,float pressure); }
interface IDisplayElement { void display(); }
3. 主题接口实现类
```csharp
public class WeatherData:ISubject
{
private float tempeature; // 温度
private float humidity;// 湿度
private float pressure; // 压力
private List<IObserver> observers;// 观察者列表,存储所有的观察者
public WeatherData(){
observers=new List<IObserver>();
}
#region 继承 ISubject 接口
public void RegisterObserver(IObserver o)
{
observers.Add(o);
}
public void RemoveObserver(IObserver o)
{
int i=observers.IndexOf(o);
observers.RemoveAt(i);
}
public void notifyObservers()
{
foreach(IObserver o in observers)
{
o.Update(temperature,humidity,pressure);
}
}
#endrgion
// 数据内容发生变化时,通知观察者发生改变
public void SetMeasurements(string temperature,string humidity,string pressure)
{
float temp;
float.TryParse(temperature,out temp);
this.tempreature=temp;
float.TryParse(humidity,out temp);
this.humidity=temp;
float.TryParse(humidity,out temp);
this.humidity=temp;
this.pressure=temp;
measurementsChanged();
}
public void measurementsChanged()
{
notifyObservers();
}
}
具体观察者
public class CurrentObserver:IObserver,IDisplayElement { private float temperature; private float humidity; private void Update(float temperature,float humidity,float pressure) { this.tempreature=temperature; this.humidity=humidity; display(); } public void didplay() { Console.WriteLine("现状:"+temperature+"F度 和"+humidity+"% 湿度"); } }客户端实现 ```csharp static void Main(string[] args) { int n=Convert.ToInt32(Console.ReadLine()); WeatherData data=new WeatehrData(); data.RegisterObserver(new CurrentObserver());
for(int i=0;i<n;i++) {
string str=Console.ReadLine(); string[] arr=str.Split(','); data.setMeasurements(arr[0],arr[1],arr[2]);}
}
二、委托事件模式<br /> 1. 主题类
```csharp
// 委托充当观察者接口类
pubic deletegate void NotifyEventHandler(object sender);
public class WeatherData
{
public Event NotifyEventHandler NotifyEvent;
public float Temperature {get;set;} // 温度
public float Humidity {get;set;} // 湿度
public float Pressure {get;set;} // 压力
public WeatherData(temperature,humidity,presure)
{
this.Temperature=temperature;
this.Humidity=humidity;
this.Pressure=presue;
}
public void RegisterObserver(NotifyEventHandler obj)
{
NotifyEventHandler+=obj;
}
public void RemoveObserver(NotifyEventHandler obj)
{
NotifyEventHandler-=obj;
}
public void Update()
{
if(NotifyEvent!=null)
{
NotifyEvent(this);
}
}
}
观察者类
public class Observer { public Update(object obj) { var ob=obj as WeatherData; if(ob!=null) { Console.writeLine("temp"+ob.Temperature); } } }客户端实现
class Program{ static void Main(String[] args) { WeatherData weather=new WeatherData(1,2,3); Observer o1=new Observer(); weather.RegisterObserver(new NotifyEventHandler(o1.Update)); weather.Update(); Console.ReadyKey(); } }
三、IObservable
