/// <summary>
/// 媒体信息,Subject对象
/// </summary>
public interface IMedia
{
void AddSubscriber(ISubscribr ssb);
void RemoveSubscriber(ISubscribr ssb);
/// <summary>
/// 有新的媒体信息来了,通知订阅者
/// </summary>
void Update();
}
/// <summary>
/// 游戏媒体信息,Subject对象
/// </summary>
public class GameMedia : IMedia
{
private List<ISubscribr> lstSsb = new List<ISubscribr>();
public void AddSubscriber(ISubscribr ssb)
{
lstSsb.Add(ssb);
}
public void RemoveSubscriber(ISubscribr ssb)
{
lstSsb.Remove(ssb);
}
public void Update()
{
foreach (var item in lstSsb)
{
item.Handle("魔兽十大经典战役视频开放下载!");
}
}
}
/// <summary>
/// 体育新闻
/// </summary>
public class SportsMedia : IMedia
{
List<ISubscribr> lstSsb = new List<ISubscribr>();
public void AddSubscriber(ISubscribr ssb)
{
lstSsb.Add(ssb);
}
public void RemoveSubscriber(ISubscribr ssb)
{
lstSsb.Remove(ssb);
}
public void Update()
{
foreach (var item in lstSsb)
{
item.Handle("今天中国夺得第13枚金牌!");
}
}
}
/// <summary>
/// 订阅者,Observer对象
/// </summary>
public interface ISubscribr
{
/// <summary>
/// 订阅者开始处理媒体信息:阅读,记录,或者扔掉
/// </summary>
/// <param name="msg"></param>
void Handle(string msg);
}
public class RssSubsriber : ISubscribr
{
public void Handle(string msg)
{
Console.WriteLine("RSS订阅者正在阅读信息:" + msg);
}
}
public class MobileSubscriber : ISubscribr
{
public void Handle(string msg)
{
Console.WriteLine("手机订阅者正在阅读信息:" + msg);
}
}
public static void ObserverPattern()
{
//消息源,即subject
IMedia gameMedia = new GameMedia();
IMedia sportsMedia = new SportsMedia();
//订阅者,即observer
//rss 订阅了游戏和运动消息
ISubscribr rss = new RssSubsriber();
gameMedia.AddSubscriber(rss);
sportsMedia.AddSubscriber(rss);
//手机订阅者订阅了运动信息
ISubscribr mobile = new MobileSubscriber();
sportsMedia.AddSubscriber(mobile);
//消息更新了,通知相关订阅者
gameMedia.Update();
sportsMedia.Update();
//说明:观察者模式类似一种消息通知机制,一组对象GroupA对另一组对象GroupB表示关注,
//需要在GroupB状态变化时自身有所反应,这时就可以使用观察者模式。
//类似的应用:1,消息通知。2,异常监控
}
/// 媒体信息,Subject对象
/// </summary>
public interface IMedia
{
void AddSubscriber(ISubscribr ssb);
void RemoveSubscriber(ISubscribr ssb);
/// <summary>
/// 有新的媒体信息来了,通知订阅者
/// </summary>
void Update();
}
/// <summary>
/// 游戏媒体信息,Subject对象
/// </summary>
public class GameMedia : IMedia
{
private List<ISubscribr> lstSsb = new List<ISubscribr>();
public void AddSubscriber(ISubscribr ssb)
{
lstSsb.Add(ssb);
}
public void RemoveSubscriber(ISubscribr ssb)
{
lstSsb.Remove(ssb);
}
public void Update()
{
foreach (var item in lstSsb)
{
item.Handle("魔兽十大经典战役视频开放下载!");
}
}
}
/// <summary>
/// 体育新闻
/// </summary>
public class SportsMedia : IMedia
{
List<ISubscribr> lstSsb = new List<ISubscribr>();
public void AddSubscriber(ISubscribr ssb)
{
lstSsb.Add(ssb);
}
public void RemoveSubscriber(ISubscribr ssb)
{
lstSsb.Remove(ssb);
}
public void Update()
{
foreach (var item in lstSsb)
{
item.Handle("今天中国夺得第13枚金牌!");
}
}
}
/// <summary>
/// 订阅者,Observer对象
/// </summary>
public interface ISubscribr
{
/// <summary>
/// 订阅者开始处理媒体信息:阅读,记录,或者扔掉
/// </summary>
/// <param name="msg"></param>
void Handle(string msg);
}
public class RssSubsriber : ISubscribr
{
public void Handle(string msg)
{
Console.WriteLine("RSS订阅者正在阅读信息:" + msg);
}
}
public class MobileSubscriber : ISubscribr
{
public void Handle(string msg)
{
Console.WriteLine("手机订阅者正在阅读信息:" + msg);
}
}
public static void ObserverPattern()
{
//消息源,即subject
IMedia gameMedia = new GameMedia();
IMedia sportsMedia = new SportsMedia();
//订阅者,即observer
//rss 订阅了游戏和运动消息
ISubscribr rss = new RssSubsriber();
gameMedia.AddSubscriber(rss);
sportsMedia.AddSubscriber(rss);
//手机订阅者订阅了运动信息
ISubscribr mobile = new MobileSubscriber();
sportsMedia.AddSubscriber(mobile);
//消息更新了,通知相关订阅者
gameMedia.Update();
sportsMedia.Update();
//说明:观察者模式类似一种消息通知机制,一组对象GroupA对另一组对象GroupB表示关注,
//需要在GroupB状态变化时自身有所反应,这时就可以使用观察者模式。
//类似的应用:1,消息通知。2,异常监控
}