设计模式之python版(十)--观察者模式
来源:互联网 发布:oracle数据字典怎么用 编辑:程序博客网 时间:2024/06/16 17:37
观察者模式:
场景特性:
结构特性:
#!/usr/bin/env python #encoding: utf-8class subject: def __init__(self): self.obs = [] def add_ob(self, ob): self.obs.append(ob) def del_ob(self, ob): self.obs.remove(ob) def notify(self): for ob in self.obs: ob.update()class observer: def __init__(self): pass def update(self): pass class create_subject(subject): def __init__(self): subject.__init__(self) self.substatue = '' class create_ob(observer): def __init__(self, name, csubj): self.name = name self.status = '' self.csubj = csubj def update(self): print '观察者{%s}的新状态是{%s}' % (self.name, self.csubj.substatue) if '__main__' == __name__: csub = create_subject() csub.add_ob(create_ob('A', csub)) csub.add_ob(create_ob('B', csub)) csub.add_ob(create_ob('C', csub)) csub.substatue = 'something happen' csub.notify()结果:
观察者{A}的新状态是{something happen}观察者{B}的新状态是{something happen}观察者{C}的新状态是{something happen}
- 设计模式之python版(十)--观察者模式
- 设计模式之十:观察者模式(Observer)
- Java设计模式之十(观察者模式)
- 设计模式(十)观察者模式
- 设计模式(二十) 观察者模式
- 设计模式—观察者模式(十)
- 设计模式(十):观察者模式
- python设计模式之观察者模式
- python设计模式之观察者模式
- 设计模式之观察者模式(Python实现)
- 设计模式:观察者模式(Python)
- <十>读<<大话设计模式>>之观察者模式
- 大话设计模式(Python版)--观察者模式
- Python设计模式-观察者模式
- python设计模式(观察者模式)
- (十)观察者模式
- 设计模式之观察者模式 (原创)
- 设计模式之Observer(观察者模式)
- js技巧代码
- 批处理同步Chrome的设置到Ramdisk
- 函数指针和指针函数
- 开篇
- main函数和启动例程
- 设计模式之python版(十)--观察者模式
- 解决VS2008 开发Windows Mobile 项目生成速度慢的问题
- vbox上新建redhat linux系统启动报错
- Warning[Pa082]:undefined behavior: the order of volatile accesses is undefined in this statement
- 黑马程序员---C#中关于方法的重写
- 数据结构:单链表的实现与Joseph环
- java study 003--the method is the same, resolve way---inner class
- 乾坤大挪移——使用PQ分区魔术师扩大C盘空间
- CMOS跳线短接后,开机密码仍存在解决方法