观察者模式是我们经常用的一个模式,今天做一个订报纸的小例子来理解一下观察者模式
出版者+订阅者=观察者模式
用下边的图来说就是 人民日报+订阅者=观察者模式
只要是订阅了人民日报的人,有了新报纸就会送到订阅者那里去
下面我们就来用代码实现一下这个模式
//报纸接口 public interface INewsPaper { //添加订阅者 void RegisterSubscriber(ISubScribe
f_subScribe); //取消订阅 void RemoveSubScriber(ISubScribe
f_subScribe); //发送报纸 void SendPaper(); } //订阅者 public interface ISubScribe { //有新的报纸了就会被执行通知 void HasNewPaper(); } //人民日报 public class PeopleNewsPaper
: INewsPaper { private List<ISubScribe>
subList = new List<ISubScribe>(); public void RegisterSubscriber(ISubScribe
f_subScribe) { subList.Add(f_subScribe); } public void RemoveSubScriber(ISubScribe
f_subScribe) { if (subList.IndexOf(f_subScribe)
>= 0) { subList.Remove(f_subScribe); } } //发报纸啦~~ public void SendPaper() { foreach (ISubScribe
_sub in subList) { _sub.HasNewPaper(); } }
} public class subHuman
: ISubScribe { //订阅者的名字 private string p_name; public subHuman( string f_name) { p_name
= f_name; } //告诉订阅者有新报纸了 public void HasNewPaper() { Console.WriteLine(p_name
+ "!!
有新的报纸了,请查收!" ); } } |
开始订订阅,和调用了
static void Main( string []
args) { PeopleNewsPaper
_paper = new PeopleNewsPaper(); subHuman
_XiaoMing = new subHuman( "小明" ); subHuman
_ZhaoYun = new subHuman( "赵云" ); subHuman
_LiuBei = new subHuman( "刘备" ); //小明订报 _paper.RegisterSubscriber(_XiaoMing); //赵云订报 _paper.RegisterSubscriber(_ZhaoYun); //刘备订报 _paper.RegisterSubscriber(_LiuBei); //有新报纸了 _paper.SendPaper(); Console.WriteLine( "---------------发完报纸了------------------" ); //小明不想订了,取消报纸 _paper.RemoveSubScriber(_XiaoMing); //又有新报纸了
就没有小明的报纸 了 _paper.SendPaper(); Console.ReadLine(); } |