设计模式-行为设计模式:观察者模式 Observer
来源:互联网 发布:网络信息员岗位职责 编辑:程序博客网 时间:2024/05/17 23:26
观察者模式,就是某件事发生之后,会起连锁反应,发生一系列的事
通知接口
public interface INotify { void Notify(); }
观察者类
public class Teacher:INotify { public void Moshou() { Console.WriteLine("老师来没收了手机"); } public void Notify() { this.Moshou(); } }
public class Registrar:INotify { public void Jiaojiazhang() { Console.WriteLine("教务主任叫家长"); } public void Notify() { this.Jiaojiazhang(); } }
public class Parent:INotify { public void Pangzou() { Console.WriteLine("家长带回家一顿胖揍"); } public void Notify() { this.Pangzou(); } }
触发事件类
public class Student { public void GetPhone() { Console.WriteLine("学生上课拿出手机来玩"); if (inotifyList != null && inotifyList.Count > 0) { foreach (INotify item in inotifyList) { item.Notify(); } } } public List<INotify> inotifyList = new List<INotify>(); public void AddInotify(INotify inotify) { inotifyList.Add(inotify); } }
调用
Student stu = new Student(); stu.AddInotify(new Teacher()); stu.AddInotify(new Registrar()); stu.AddInotify(new Parent()); stu.GetPhone();
0 0
- [设计模式-行为型]观察者模式(Observer)
- 行为设计模式---观察者模式(Observer)
- 【设计模式】行为模式之Observer观察者
- 设计模式-行为设计模式:观察者模式 Observer
- 设计模式深入学习--Observer 观察者模式(行为型模式)
- 设计模式深入学习--Observer 观察者模式(行为型模式)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 设计模式----行为型模式之观察者模式(Observer Pattern)
- 观察者设计模式Observer
- 观察者设计模式(observer)
- observer(观察者设计模式)
- Observer(观察者)设计模式
- Leetcode Min Stack
- 股票入门基本知识5:通过交易方式选股
- web服务中三种发送接受协议SOAP/HTTP
- Android -- sp、wp智能指针的原理简介
- Visualizing GoogLeNet Classes[译]
- 设计模式-行为设计模式:观察者模式 Observer
- javascript 20161218
- (ZT)设置wifi热点的bat脚本
- MFC的资源切换AFX_MANAGE_STATE (转载http://www.cnblogs.com/ntrgx/archive/2010/11/14/1877199.html)
- Codeforces 745C 并查集+贪心
- Spring MVC的一些关于请求的注解用法详解
- JSP笔记乱(二)
- 20161218
- [Java]java中Map和List初始化的两种方法