设计模式笔记之 观察者模式
观察者模式
代码:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObserverPattern
{
class Program
{
static Subject subject=new Subject();//一个被观察者
static void Main(string[] args)
{
ObserverDispalyInfor oneObserverDisplayInfor = new ObserverDispalyInfor();
ObserverForecast oneObserverForecast = new ObserverForecast();
subject.RegisterObserver(oneObserverDisplayInfor);//布告观察者注册
subject.RegisterObserver(oneObserverForecast);//天气预报观察者注册
subject.GetChange(12, 23, 34);
subject.RemoveRegisterObserver(oneObserverDisplayInfor);//布告观察者离开了
subject.GetChange(12, 23, 666);
}
}
/// <summary>
/// 被观察者接口
/// </summary>
public interface ISubject
{
void RegisterObserver(IObserver observer);
void RemoveRegisterObserver(IObserver observer);
void Notify();
}
/// <summary>
/// 观察者接口
/// </summary>
public interface IObserver
{
void Updata(int temperature,int humidity,int pressure);
}
/// <summary>
/// 布告板的显示动作接口
/// </summary>
public interface IDisplay
{
void DisPlay();
}
/// <summary>
/// 被观察者类
/// </summary>
public class Subject
{
List<IObserver> observerList;
private int temperature;
private int humidity;
private int pressure;
public Subject()
{
this.observerList = new List<IObserver>();
}
/// <summary>
/// 注册一个观察者
/// </summary>
/// <param name="observer"></param>
public void RegisterObserver(IObserver observer)
{
this.observerList.Add(observer);
Console.Write(observer.ToString() + "注册了,热烈欢迎\r\n");
Console.Read();
Console.Read();
}
/// <summary>
/// 去掉一个注册者
/// </summary>
/// <param name="observer"></param>
public void RemoveRegisterObserver(IObserver observer)
{
foreach (IObserver oneObserver in this.observerList)
{
if (oneObserver == observer)
{
this.observerList.Remove(observer);
Console.Write(observer.ToString() + "走了,希望以后还有合作的机会\r\n");
Console.Read();
Console.Read();
break;
}
}
}
/// <summary>
/// 通知观察者方法
/// </summary>
public void Notify()
{
Console.Write("参数发生了变化,我要通知给观察者\r\n");
Console.Read();
Console.Read();
foreach (IObserver oneObserver in this.observerList)
{
oneObserver.Updata(this.temperature,this.humidity,this.pressure);
}
}
/// <summary>
/// 改变状态
/// </summary>
public void GetChange(int temperature,int humidity,int pressure)
{
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
this.Notify();//通知观察者
}
}
/// <summary>
/// 布告观察者
/// </summary>
public class ObserverDispalyInfor:IObserver,IDisplay
{
private int temperature;
private int humidity;
private int pressrue;
private DateTime notifyTime;
public ObserverDispalyInfor()
{
this.notifyTime = new DateTime();
}
public void Updata(int temperature, int humidity, int pressure)
{
this.temperature = temperature;
this.humidity = humidity;
this.pressrue = pressure;
this.notifyTime = DateTime.Now;
this.DisPlay();
}
public void DisPlay()
{
Console.Write("----" + this.ToString() + "接受到了新消息-----\r\n");
System.Console.Write("气象部门" + this.notifyTime.ToString() + "发布的消息\r\n"
+"温度:"+this.temperature.ToString()
+"\r\n湿度:"+this.humidity.ToString()
+"\r\n压力:"+this.pressrue.ToString());
Console.Read();
Console.Read();
}
}
/// <summary>
/// 天气预报观察者
/// </summary>
public class ObserverForecast : IObserver, IDisplay
{
private int temperature;
private int humidity;
private int pressrue;
private bool rain;
public void Updata(int temperature, int humidity, int pressure)
{
this.temperature = temperature;
this.humidity = humidity;
this.pressrue = pressure;
this.DisPlay();
}
/// <summary>
/// 天气预报观察者利用被观察者通知得到的信息计算出是不是要下雨
/// </summary>
private void Calculate()
{
if (this.temperature + this.humidity + this.pressrue > 100)
{
this.rain = true;
}
else
{
this.rain = false;
}
}
public void DisPlay()
{
this.Calculate();
string temp = string.Empty;
if (this.rain)
{
temp = "下雨了,打雷收衣服啊\r\n";
}
else
{
temp = "今天天气不错,风和日丽的\r\n";
}
Console.Write("----"+this.ToString() + "接受到了新消息-----\r\n");
System.Console.Write(temp);
Console.Read();
Console.Read();
}
}
}
运行后:
欢迎转载,转载请注明出处