下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
__author__ = 'administrator'#/usr/env/bin python ''' this is document'''class Event(object): ''' 事件初始化的一个方式 ''' def __init__(self,event_type,data=None): self._type = event_type self._data = data @property def type(self): return self._type @property def data(self): return self._data class Eventdispatcher(object): """ event分发类 监听和分发event事件 """ def __init__(self): #初始化事件 self._events = dict() def __del__(self): self._events = None def has_Listener(self,Listener): if event_type in self._events.keys(): return Listener in self._events[event_type] else: return False def dispatch_event(self,event): """ dispatch an instance of Event class """ # 分发event到所有关联的Listener if event.type in self._events.keys(): Listeners = self._events[event.type] for Listener in Listeners: Listener(event) def add_event_Listener(self,Listener): #给某种事件类型添加Listner if not self.has_Listener(event_type,Listener): Listeners = self._events.get(event_type,[]) Listeners.append(Listener) self._events[event_type] = Listeners def remove_event_Listener(self,Listener): if self.has_Listener(event_type,Listener): Listeners = self._events[event_type] if len(Listeners) == 1: del self._events[event_type] else: Listeners.remove(Listener) self._events[event_type] = Listeners class MyEvent(Event): ASK = "askMyEvent" RESPOND = "respondMyEvent" class WhoAsk(object): def __init__(self,event_dispatcher): self.event_dispatcher = event_dispatcher self.event_dispatcher.add_event_Listener( MyEvent.RESPOND,self.on_answer_event ) def ask(self): print("who are Listener to me?") self.event_dispatcher.dispatch_event(MyEvent(MyEvent.ASK,self)) def on_answer_event(self,event): print("receive event %s",event.data) class WhoRespond(object): def __init__(self,event_dispatcher): self.event_dispatcher = event_dispatcher self.event_dispatcher.add_event_Listener(MyEvent.ASK,self.on_ask_event) def on_ask_event(self,event): self.event_dispatcher.dispatch_event(MyEvent(MyEvent.RESPOND,self)) dispatcher = Eventdispatcher()who_ask = WhoAsk( dispatcher )who_responde1 = WhoRespond( dispatcher )who_responde2 = WhoRespond( dispatcher ) # WhoAsk askwho_ask.ask()
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的python事件驱动模型的代码全部内容,希望文章能够帮你解决python事件驱动模型的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)