观察者设计模式
定义
观察者设计模式:观察者设计模式中包含一对多的依赖关系,让多个观察者同时关注某一主题,当这个主题的某一状态发生变化,会通知所有观察着对象,使观察者能够自动更新自身。
解决问题
观察者设计模式主要是以一种简单易行的方法而又灵活的方式解决一对多的通知问题。
角色
抽象主题(abstractSubject):保存一个观察者集合,可以增加与删除观察者对象,并能通知观察者对象。
具体主题(Subject):继承抽象主题,拥有具体的主题状态。
通知接口(INotify):观察着实现的通知到达时统一更新自身接口。
观察者(ConcreateObserver):实现通知接口的具体类。
类图
实现代码
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 抽象主题
/// </summary>
public abstract class AbstractSubject
{
/// <summary>
/// 观察者列表
/// </summary>
private List<INotify> ObserverList = new List<INotify> ();
/// <summary>
/// 注册观察着
/// </summary>
/// <param name="observer"></param>
public void Register(INotify observer)
{
ObserverList.Add(observer);
Console.WriteLine("Register:{0}", observer.Name);
}
/// <summary>
/// 移除观察者
/// </summary>
/// <param name="observer"></param>
public void UnRegister(INotify observer)
{
ObserverList.Remove (observer);
Console.WriteLine("UnRegister:{0}", observer.Name);
}
/// <summary>
/// 通知观察者
/// </summary>
public void NotifyObserver()
{
for (int i = 0; i < ObserverList.Count; i++)
{
ObserverList[i].Notify(this);
}
}
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 具体主题
/// </summary>
public class ConcreateSubject : AbstractSubject
{
/// <summary>
/// 状态
/// </summary>
private string state;
public string State
{
get
{
return state;
}
set
{
state = value;
}
}
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 消息通知接口
/// </summary>
public interface INotify
{
/// <summary>
/// 名称
/// </summary>
string Name
{
get;
set;
}
/// <summary>
/// 通知
/// </summary>
/// <param name="sender"></param>
void Notify(object sender);
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 观察着B
/// </summary>
public class ConcreateObserverA : INotify
{
private string name;
public ConcreateObserverA(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
#region INotify 成员
public void Notify(object sender)
{
ConcreateSubject sub=sender as ConcreateSubject ;
string info = string.Format("Type:{0} Name:{1} Notify:{2}", this.GetType().FullName, Name, sub.State.ToString());
Console.WriteLine(info);
}
#endregion
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 观察者B
/// </summary>
public class ConcreateObserverB : INotify
{
private string name;
public ConcreateObserverB(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
#region INotify 成员
public void Notify(object sender)
{
ConcreateSubject sub = sender as ConcreateSubject;
string info = string.Format("Type:{0} Name:{1} Notify:{2}", this.GetType().FullName, Name, sub.State );
Console.WriteLine(info);
}
#endregion
}
}
调用
namespace Hbb0b0.DesignPatter
{
class ObserverMain
{
static internal void Run()
{
//具体主题
ConcreateSubject subject = new ConcreateSubject();
//观察者A1
ConcreateObserverA observerA1 = new ConcreateObserverA("A1");
//注册A1
subject.Register(observerA1);
//观察者A2
ConcreateObserverA observerA2 = new ConcreateObserverA("A2");
//注册A2
subject.Register(observerA2);
//观察着B1
ConcreateObserverB observerB1 = new ConcreateObserverB("B1");
//注册B1
subject.Register(observerB1);
//观察者B2
ConcreateObserverB observerB2 = new ConcreateObserverB("B2");
//注册者B2
subject.Register(observerB2);
//通知观察者
//主题状态更新
subject.State = "State A";
subject.NotifyObserver();
//注销观察者
subject.UnRegister(observerA2);
subject.UnRegister(observerB1);
//通知观察者
//主题状态更新
subject.State ="State B";
subject.NotifyObserver();
}
}
}
{
/// <summary>
/// 抽象主题
/// </summary>
public abstract class AbstractSubject
{
/// <summary>
/// 观察者列表
/// </summary>
private List<INotify> ObserverList = new List<INotify> ();
/// <summary>
/// 注册观察着
/// </summary>
/// <param name="observer"></param>
public void Register(INotify observer)
{
ObserverList.Add(observer);
Console.WriteLine("Register:{0}", observer.Name);
}
/// <summary>
/// 移除观察者
/// </summary>
/// <param name="observer"></param>
public void UnRegister(INotify observer)
{
ObserverList.Remove (observer);
Console.WriteLine("UnRegister:{0}", observer.Name);
}
/// <summary>
/// 通知观察者
/// </summary>
public void NotifyObserver()
{
for (int i = 0; i < ObserverList.Count; i++)
{
ObserverList[i].Notify(this);
}
}
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 具体主题
/// </summary>
public class ConcreateSubject : AbstractSubject
{
/// <summary>
/// 状态
/// </summary>
private string state;
public string State
{
get
{
return state;
}
set
{
state = value;
}
}
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 消息通知接口
/// </summary>
public interface INotify
{
/// <summary>
/// 名称
/// </summary>
string Name
{
get;
set;
}
/// <summary>
/// 通知
/// </summary>
/// <param name="sender"></param>
void Notify(object sender);
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 观察着B
/// </summary>
public class ConcreateObserverA : INotify
{
private string name;
public ConcreateObserverA(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
#region INotify 成员
public void Notify(object sender)
{
ConcreateSubject sub=sender as ConcreateSubject ;
string info = string.Format("Type:{0} Name:{1} Notify:{2}", this.GetType().FullName, Name, sub.State.ToString());
Console.WriteLine(info);
}
#endregion
}
}
namespace Hbb0b0.DesignPatter
{
/// <summary>
/// 观察者B
/// </summary>
public class ConcreateObserverB : INotify
{
private string name;
public ConcreateObserverB(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
#region INotify 成员
public void Notify(object sender)
{
ConcreateSubject sub = sender as ConcreateSubject;
string info = string.Format("Type:{0} Name:{1} Notify:{2}", this.GetType().FullName, Name, sub.State );
Console.WriteLine(info);
}
#endregion
}
}
调用
namespace Hbb0b0.DesignPatter
{
class ObserverMain
{
static internal void Run()
{
//具体主题
ConcreateSubject subject = new ConcreateSubject();
//观察者A1
ConcreateObserverA observerA1 = new ConcreateObserverA("A1");
//注册A1
subject.Register(observerA1);
//观察者A2
ConcreateObserverA observerA2 = new ConcreateObserverA("A2");
//注册A2
subject.Register(observerA2);
//观察着B1
ConcreateObserverB observerB1 = new ConcreateObserverB("B1");
//注册B1
subject.Register(observerB1);
//观察者B2
ConcreateObserverB observerB2 = new ConcreateObserverB("B2");
//注册者B2
subject.Register(observerB2);
//通知观察者
//主题状态更新
subject.State = "State A";
subject.NotifyObserver();
//注销观察者
subject.UnRegister(observerA2);
subject.UnRegister(observerB1);
//通知观察者
//主题状态更新
subject.State ="State B";
subject.NotifyObserver();
}
}
}