在现实世界中,没有单纯的观察者或者被观察者,大部分时候,一体两种角色兼备。
一个小小的实验:
Observer
namespace woodigg.DesignMode
{
/// <summary>
/// 通知对象
/// </summary>
public abstract class Human
{
/// <summary>
/// 名字
/// </summary>
public string Name { get; set; }
/// <summary>
/// Constructor
/// </summary>
public Human(string name){
this.Name = name;
}
/// <summary>
/// 委托
/// </summary>
/// <param name="name"></param>
public delegate void HumanEventHandler(string name);
public event HumanEventHandler HumanEvent;
/// <summary>
/// 通知
/// </summary>
public virtual void Notify() {
if (HumanEvent != null)
HumanEvent(this.Name);
}
}
/// <summary>
/// 观察者对象
/// </summary>
public abstract class Observer4Human : Human
{
/// <summary>
/// 名字传递给基类
/// </summary>
/// <param name="name"></param>
public Observer4Human(string name) : base(name) {}
/// <summary>
/// 通知回调方法
/// </summary>
/// <param name="name"></param>
public abstract void Update(string name);
}
//--------------------- Concrete -------------------------//
/// <summary>
/// 丈夫
/// </summary>
public class Husband : Observer4Human
{
public Husband(string name) : base(name) { }
public override void Notify()
{
Console.WriteLine(this.Name + " 喝多了!");
base.Notify();
}
public override void Update(string name)
{
Console.WriteLine(this.Name + " 看着 "+name+" 不再闹了!");
}
}
/// <summary>
/// 妻子
/// </summary>
public class Wife : Observer4Human {
public Wife(string name) : base(name) { }
public override void Update(string name)
{
Console.WriteLine(this.Name + " 被 "+name+" 吵醒了!");
base.Notify();
}
}
/// <summary>
/// 孩子
/// </summary>
public class Baby : Observer4Human
{
public Baby(string name) : base(name) { }
public override void Update(string name)
{
Console.WriteLine(this.Name + " 被 "+name+" 吓哭了!");
base.Notify();
}
}
}
{
/// <summary>
/// 通知对象
/// </summary>
public abstract class Human
{
/// <summary>
/// 名字
/// </summary>
public string Name { get; set; }
/// <summary>
/// Constructor
/// </summary>
public Human(string name){
this.Name = name;
}
/// <summary>
/// 委托
/// </summary>
/// <param name="name"></param>
public delegate void HumanEventHandler(string name);
public event HumanEventHandler HumanEvent;
/// <summary>
/// 通知
/// </summary>
public virtual void Notify() {
if (HumanEvent != null)
HumanEvent(this.Name);
}
}
/// <summary>
/// 观察者对象
/// </summary>
public abstract class Observer4Human : Human
{
/// <summary>
/// 名字传递给基类
/// </summary>
/// <param name="name"></param>
public Observer4Human(string name) : base(name) {}
/// <summary>
/// 通知回调方法
/// </summary>
/// <param name="name"></param>
public abstract void Update(string name);
}
//--------------------- Concrete -------------------------//
/// <summary>
/// 丈夫
/// </summary>
public class Husband : Observer4Human
{
public Husband(string name) : base(name) { }
public override void Notify()
{
Console.WriteLine(this.Name + " 喝多了!");
base.Notify();
}
public override void Update(string name)
{
Console.WriteLine(this.Name + " 看着 "+name+" 不再闹了!");
}
}
/// <summary>
/// 妻子
/// </summary>
public class Wife : Observer4Human {
public Wife(string name) : base(name) { }
public override void Update(string name)
{
Console.WriteLine(this.Name + " 被 "+name+" 吵醒了!");
base.Notify();
}
}
/// <summary>
/// 孩子
/// </summary>
public class Baby : Observer4Human
{
public Baby(string name) : base(name) { }
public override void Update(string name)
{
Console.WriteLine(this.Name + " 被 "+name+" 吓哭了!");
base.Notify();
}
}
}
测试代码:
代码
[Test]
public void HumanObserverTest()
{
//通知者
Husband husband = new Husband("Johnny");
Observer4Human wife = new Wife("Have");
Observer4Human babe = new Baby("kitt");
husband.HumanEvent +=new Human.HumanEventHandler(wife.Update);
wife.HumanEvent += new Human.HumanEventHandler(babe.Update);
babe.HumanEvent += new Human.HumanEventHandler(husband.Update);
husband.Notify();
}
public void HumanObserverTest()
{
//通知者
Husband husband = new Husband("Johnny");
Observer4Human wife = new Wife("Have");
Observer4Human babe = new Baby("kitt");
husband.HumanEvent +=new Human.HumanEventHandler(wife.Update);
wife.HumanEvent += new Human.HumanEventHandler(babe.Update);
babe.HumanEvent += new Human.HumanEventHandler(husband.Update);
husband.Notify();
}
NUnit结果:
***** woodigg.DesignMode.ObserverTest.AbstractObserverTest.HumanObserverTest
Johnny 喝多了!
Have 被 Johnny 吵醒了!
kitt 被 Have 吓哭了!
Johnny 看着 kitt 不再闹了!
Johnny 喝多了!
Have 被 Johnny 吵醒了!
kitt 被 Have 吓哭了!
Johnny 看着 kitt 不再闹了!