【2016-10-10】【坚持学习】【Day1】【观察者模式】
今天学习了观察者模式
定义:
一个实体变化会影响其他实体变化
例子:
红绿灯与汽车
红绿灯是观察目标
汽车是实际观察者
灯的变化会影响车是停止还是前进。
例子:
游戏中,一个战队由若干队员组成,当其中一个受到攻击,就要通过战队中心呼叫其他队友支援。
- using System.Collection
- abstract class Subject
- {
- //定义一个观察者集合用于存储所有观察者对象
- protected ArrayList observers = new ArrayList();
- //声明抽象注册方法,用于向观察者集合中增加一个观察者
- public abstract void Attach(Observer observer);
- //声明抽象注销方法,用于在观察者集合中删除一个观察者
- public abstract void Detach(Observer observer);
- //声明抽象通知方法
- public abstract void Notify();
- }
- class ConcreteSubject : Subject
- {
- public override void Attach(Observer observer)
- {
- observers.Add(observer);
- }
- public override void Detach(Observer observer)
- {
- observers.Remove(observer);
- }
- //实现通知方法
- public override void Notify()
- {
- //遍历观察者集合,调用每一个观察者的响应方法
- foreach(object obs in observers)
- {
- ((Observer)obs).Update();
- }
- }
- }
- interface Observer
- {
- void Update();
- }
- class ConcreteObserver : Observer
- {
- //实现响应方法
- public void Update()
- {
- //具体更新代码
- }
- }
调用
- ……
- Subject subject = new ConcreteSubject();
- Observer observer = new ConcreteObserver();
- subject.Attach(observer);
- subject.Notify();
- ……
【2016-10-10】【坚持学习】【Day1】【观察者模式】的更多相关文章
- 2016/11/10 吃吃喝喝Hacking Thursday Night聚餐活动 at Dunkin Donuts
店名:Dunkin Donuts 唐恩都乐 点评:http://www.dianping.com/shop/21378231 地址:静安区南京西路1649号静安公园内(近静安公园) 走法:地铁2号线静 ...
- 微信iphone7、 ios10播放视频解决方案 2016.11.10
2016.11.10日更新以下方法 微信最新出同层播放规范 即使是官方的也无法解决所有android手机的问题. 另外iphone 5 .5s 某些手机始终会弹出播放,请继续采用 “以下是老的解决办法 ...
- 2016年10月31日 星期一 --出埃及记 Exodus 19:16
2016年10月31日 星期一 --出埃及记 Exodus 19:16 On the morning of the third day there was thunder and lightning, ...
- 2016年10月30日 星期日 --出埃及记 Exodus 19:15
2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...
- 2016年10月29日 星期六 --出埃及记 Exodus 19:14
2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...
- 2016年10月28日 星期五 --出埃及记 Exodus 19:13
2016年10月28日 星期五 --出埃及记 Exodus 19:13 He shall surely be stoned or shot with arrows; not a hand is to ...
- 2016年10月27日 星期四 --出埃及记 Exodus 19:12
2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...
- 2016年10月26日 星期三 --出埃及记 Exodus 19:10-11
2016年10月26日 星期三 --出埃及记 Exodus 19:10-11 And the LORD said to Moses, "Go to the people and consec ...
- 2016年10月25日 星期二 --出埃及记 Exodus 19:9
2016年10月25日 星期二 --出埃及记 Exodus 19:9 The LORD said to Moses, "I am going to come to you in a dens ...
- 2016年10月24日 星期一 --出埃及记 Exodus 19:8
2016年10月24日 星期一 --出埃及记 Exodus 19:8 The people all responded together, "We will do everything th ...
随机推荐
- Mithril – 构建杰出 Web 应用的 JS MVC 框架
Mithril 是一个客户端的 Javascript MVC 框架.它是一个工具,使应用程序代码分为数据层,UI 层和粘合层.提供了一个模板引擎与一个虚拟的 DOM diff 实现,用于高性能渲染,支 ...
- UITableViewDataSource协议
前言: 在iOS开发中,表视图UITableView 是我们做UI界面设计时的重要视图. 那么,使用表视图UITableView 需要遵守哪些协议呢? <UITableViewDataSourc ...
- Atitit.木马病毒强制强行关闭360 360tray.exe的方法
Atitit.木马病毒强制强行关闭360 360tray.exe的方法 1. taskkill /im 进程名称1 2. 用 wmic process where name="进程名称&qu ...
- Hibernate3注解[转]
Hibernate3注解 收藏 1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="&qu ...
- 微信小程序需要https后台的创业机会思考
最近比较关注微信小程序,而且微信小程序的后台必须强制要求https, https相对http成本要高很多了. 这里我感觉有2个商机 (1)提供https 中转服务器 ,按流量来收费 (2) 微信小程序 ...
- webbench 压力测试
原文 webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好用,安装使用也特别方便,并且非常小. 主要是 -t 参数用着比较爽,下面参考了张宴 ...
- umeng track 相关
NSString * appKey = @"57105bbbe0f55a7938002063"; NSString * deviceName = [[[UIDevice curre ...
- Win7重装系统遇到的问题以及MysQL的问题解决
连续三天因为系统的错误,android方面的软件一直不能正确运行.而且每次开机的时候QQ 微信等聊天工具也出现损坏.虽然重新下载一个可以保证在电脑不管的情况下正常的运行.可是作为玩电脑时间不长的我来说 ...
- C语言中的字符串
字符串 字符串 用双引号引起来的多个字符. 在C语言中字符串是用’\0’结束的.即每个字符串的最后一个字符是’\0’,但是结束符不显示,仅仅标志该字符串到这儿就结束了. 二.声明字符串 char *s ...
- iOS 9 使用HTTP的方法
问题 在ios 9中使用HTTP请求,Xcode就会抛出下面的Exception: App Transport Security has blocked a cleartext HTTP (http: ...