动机:定义对象间的一种一对多依赖关系,以便当一个对象(主体)的状态发生改变时,所有依赖于它的对象(观察者)都得到通知并自动更新。
场景:类似于SharePoint中的“通知我”功能,系统中的列表(包括普通列表、文档列表)发生改变时,可以通过多种途径通知其拥有者。各种列表和通知程序是松耦合的,并不需要互相知晓。
结构
代码
namespace DesignPattern.Observer
{
/// <summary>
/// 用户
/// </summary>
public class User
{
private string name;
private string email;
private string mobile;
private string ocsAccount;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
public string Mobile
{
get
{
return mobile;
}
set
{
mobile = value;
}
}
public string OcsAcccount
{
get
{
return ocsAccount;
}
set
{
ocsAccount = value;
}
}
}
}
{
/// <summary>
/// 用户
/// </summary>
public class User
{
private string name;
private string email;
private string mobile;
private string ocsAccount;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
public string Mobile
{
get
{
return mobile;
}
set
{
mobile = value;
}
}
public string OcsAcccount
{
get
{
return ocsAccount;
}
set
{
ocsAccount = value;
}
}
}
}
namespace DesignPattern.Observer
{
/// <summary>
/// 观察者,负责向用户多种通讯设备发送列表更改消息
/// </summary>
public interface ISender
{
void SendAnnouncement (SenderArgs args);
}
public class EmailSender : ISender
{
public void SendAnnouncement(SenderArgs args)
{
// 向地址为args.ToUser.Email的用户发送标题为args.Title,正文为args.Body的Email
}
}
public class MobileMessageSender : ISender
{
public void SendAnnouncement(SenderArgs args)
{
// 向号码为args.ToUser.Mobile的用户发送标题为args.Title,正文为args.Body的短信
}
}
public class OcsMessageSender : ISender
{
public void SendAnnouncement(SenderArgs args)
{
// 向用户名为args.ToUser.OcsAccount的用户发送标题为args.Title,正文为args.Body的OCS信息
}
}
/// <summary>
/// 包含发送消息信息
/// </summary>
public class SenderArgs
{
private User toUser;
private string title;
private string body;
public User ToUser
{
get
{
return toUser;
}
set
{
toUser = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public string Body
{
get
{
return body;
}
set
{
body = value;
}
}
public SenderArgs(User toUser, string title, string body)
{
this.toUser = toUser;
this.title = title;
this.body = body;
}
}
}
{
/// <summary>
/// 观察者,负责向用户多种通讯设备发送列表更改消息
/// </summary>
public interface ISender
{
void SendAnnouncement (SenderArgs args);
}
public class EmailSender : ISender
{
public void SendAnnouncement(SenderArgs args)
{
// 向地址为args.ToUser.Email的用户发送标题为args.Title,正文为args.Body的Email
}
}
public class MobileMessageSender : ISender
{
public void SendAnnouncement(SenderArgs args)
{
// 向号码为args.ToUser.Mobile的用户发送标题为args.Title,正文为args.Body的短信
}
}
public class OcsMessageSender : ISender
{
public void SendAnnouncement(SenderArgs args)
{
// 向用户名为args.ToUser.OcsAccount的用户发送标题为args.Title,正文为args.Body的OCS信息
}
}
/// <summary>
/// 包含发送消息信息
/// </summary>
public class SenderArgs
{
private User toUser;
private string title;
private string body;
public User ToUser
{
get
{
return toUser;
}
set
{
toUser = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public string Body
{
get
{
return body;
}
set
{
body = value;
}
}
public SenderArgs(User toUser, string title, string body)
{
this.toUser = toUser;
this.title = title;
this.body = body;
}
}
}
namespace DesignPattern.Observer
{
/// <summary>
/// 列表,即主体
/// </summary>
public abstract class List
{
protected User owner;
protected IList<ISender> senders;
public User Owner
{
get
{
return owner;
}
set
{
owner = value;
}
}
public List(User owner)
{
this.owner = owner;
senders = new List<ISender>();
}
public void Notify(SenderArgs args)
{
foreach (ISender sender in senders)
{
sender.SendAnnouncement(args);
}
}
public void AddSender(ISender sender)
{
senders.Add(sender);
}
public void RemoveReceiver(ISender sender)
{
senders.Remove(sender);
}
}
public interface IList
{
void Add(ListItem item);
void Update(ListItem item);
void Delete(ListItem item);
}
/// <summary>
/// 列表数据项
/// </summary>
public class ListItem
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
}
public class GenericList : List, IList
{
public GenericList(User owner)
: base(owner)
{
}
public void Add(ListItem item)
{
// 添加数据项
SenderArgs args = new SenderArgs(owner, "添加了标题为" + item.Title + "的列表数据项", "详情……");
Notify(args);
}
public void Update(ListItem item)
{
// 更新数据项
SenderArgs args = new SenderArgs(owner, "更新了标题为" + item.Title + "的列表数据项", "详情……");
Notify(args);
}
public void Delete(ListItem item)
{
// 删除数据项
SenderArgs args = new SenderArgs(owner, "删除了标题为" + item.Title + "的列表数据项", "详情……");
Notify(args);
}
}
public class DocumentList : List, IList
{
public DocumentList(User owner)
: base(owner)
{
}
public void Add(ListItem item)
{
// 添加文档
SenderArgs args = new SenderArgs(owner, "添加了名称为" + item.Title + "的文档", "详情……");
Notify(args);
}
public void Update(ListItem item)
{
// 更新文档
SenderArgs args = new SenderArgs(owner, "更新了标题为" + item.Title + "的文档", "详情……");
Notify(args);
}
public void Delete(ListItem item)
{
// 删除文档
SenderArgs args = new SenderArgs(owner, "删除了标题为" + item.Title + "的文档", "详情……");
Notify(args);
}
}
}
{
/// <summary>
/// 列表,即主体
/// </summary>
public abstract class List
{
protected User owner;
protected IList<ISender> senders;
public User Owner
{
get
{
return owner;
}
set
{
owner = value;
}
}
public List(User owner)
{
this.owner = owner;
senders = new List<ISender>();
}
public void Notify(SenderArgs args)
{
foreach (ISender sender in senders)
{
sender.SendAnnouncement(args);
}
}
public void AddSender(ISender sender)
{
senders.Add(sender);
}
public void RemoveReceiver(ISender sender)
{
senders.Remove(sender);
}
}
public interface IList
{
void Add(ListItem item);
void Update(ListItem item);
void Delete(ListItem item);
}
/// <summary>
/// 列表数据项
/// </summary>
public class ListItem
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
}
public class GenericList : List, IList
{
public GenericList(User owner)
: base(owner)
{
}
public void Add(ListItem item)
{
// 添加数据项
SenderArgs args = new SenderArgs(owner, "添加了标题为" + item.Title + "的列表数据项", "详情……");
Notify(args);
}
public void Update(ListItem item)
{
// 更新数据项
SenderArgs args = new SenderArgs(owner, "更新了标题为" + item.Title + "的列表数据项", "详情……");
Notify(args);
}
public void Delete(ListItem item)
{
// 删除数据项
SenderArgs args = new SenderArgs(owner, "删除了标题为" + item.Title + "的列表数据项", "详情……");
Notify(args);
}
}
public class DocumentList : List, IList
{
public DocumentList(User owner)
: base(owner)
{
}
public void Add(ListItem item)
{
// 添加文档
SenderArgs args = new SenderArgs(owner, "添加了名称为" + item.Title + "的文档", "详情……");
Notify(args);
}
public void Update(ListItem item)
{
// 更新文档
SenderArgs args = new SenderArgs(owner, "更新了标题为" + item.Title + "的文档", "详情……");
Notify(args);
}
public void Delete(ListItem item)
{
// 删除文档
SenderArgs args = new SenderArgs(owner, "删除了标题为" + item.Title + "的文档", "详情……");
Notify(args);
}
}
}
欢迎光临我的淘宝http://shop35795100.taobao.com,专营游戏点卡、电话卡及各类充值卡。