【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 ...
随机推荐
- 从0开始学angularjs-笔记04
由于公司加班,刚到家不久,然而却毫无睡意,可能是老了吧--- 不废话了,没睡意那就做点有意义的事情吧,继续我们的angular学习之路,深夜博文,希望能造福大家! 这次我们来详细讲解angular的双 ...
- sublime安装
1.sublime下载安装 下载地址:http://www.sublimetext.com/3 选择合适版本下载 2.自定义快捷键 Ctrl+Shift+P调出命令面板 3.自定义设置 Ctrl+Sh ...
- Understanding theory (1)
Source: verysmartbrothas.com It has been confusing since my first day as a PhD student about theory ...
- Android终止线程的方法
线程对象属于一次性消耗品,一般线程执行完run方法之后,线程就正常结束了,线程结束之后就报废了,不能再次start,只能新建一个线程对象.但有时run方法是永远不会结束的.例如在程序中使用线程进行So ...
- 代码创建storyboard
代码创建storyboard方式如下 // 加载storyboard UIStoryboard *storyboard = [UIStoryboard StoryboardWithName:@&quo ...
- UIApplication是什么
1.UIApplication对象是应用程序的象征 2.每一个应用都有自己的UIApplication对象,而且是单例的(只有一个) 3.通过[UIApplication sharedAppl ...
- oc程序编译【-framework Foundation 编译】
第一种编译 第二种编译 -framework Foundation 编译
- NSArray Sort 排序
打算实现tableview的生序降序排序 ```js NSArray * rs= [oneArray sortedArrayUsingComparator:NSComparisonResult(RFI ...
- XMLA ODBO 以及OLAP服务提供者自定义的协议,我们如何选择
参考 SAP给他的客户的帮助<ODBO, BAPI and XMLA - Sap> SAP BW 提供的查询接口: 接口 查询语言 调用接口 OS平台 客户端开发 ODBO MDX C ...
- VBS进行http请求及JSON数据的读取和生成
背景: 近期帮一个公司做第三方API的二次封装,需要部署到该公司网站.所获取的是Json数据格式.由于该公司原系统采用的ASP+VBS技术方案,因此采用VBS对API进行请求.封装. 实现: 废话不多 ...