23种设计模式之python实现--观察者模式
2014-04-18 09:53
489 查看
前几天,家里出了点事,一下这中断了写blog,现在继续接着写吧,从这篇开始写行为型的模式
#理解 #Observer模式是最多应用的模式之一 #在很多的编程语言中,都实现了这种模式,并利用了这种模式 #例子 #订报的例子应该比较合适吧 class publisher(object): observers=list() msg=None def register(self,observer): self.observers.append(observer) def disregister(self,observer): self.observers.remove(observer) def Notify(self): for ob in self.observers: ob.update() class newspublisher(publisher): def getMessage(self): return self.msg def setMessage(self,msg): self.msg=msg class observer: def update(self): pass class people(observer): def __init__(self,publisher): self.publisher=publisher self.publisher.register(self) def disregister(self): self.publisher.disregister(self) def update(self): print self.publisher.getMessage() if __name__=="__main__": news=newspublisher() person1=people(news) person2=people(news) news.setMessage("nihao") news.Notify() person1.disregister() news.setMessage("hello") news.Notify()
相关文章推荐
- 23种设计模式之python实现--解释器模式
- 23种设计模式之python实现--visitor模式
- 23种设计模式之python实现--Facade模式
- 23种设计模式之python实现--Template模式
- Head First 设计模式——观察者模式(Observer Pattern)——Python实现(3)
- 23种设计模式之python实现--工厂方法
- Head First 设计模式——观察者模式(Observer Pattern)——Python实现(1)
- 23种设计模式之python实现--中介者模式
- Head First 设计模式——观察者模式(Observer Pattern)——Python实现(2)
- 23种设计模式之python实现--Strategy模式
- 23种设计模式之python实现--command模式
- 23种设计模式之python实现--Bridge模式
- 【设计模式】观察者模式(observer)c#/python实现
- 23种设计模式之python实现--职责链模式
- 23种设计模式之python实现--Decorator模式
- [python实现设计模式]-4.观察者模式-吃食啦!
- 23种设计模式之python实现--代理(Proxy)模式
- 23种设计模式之python实现--Iterator模式
- Python如何实现单例模式?其他23种设计模式python如何实现?
- 23种设计模式之python实现--Memento模式