📜  python 观察者模式 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:41.848000             🧑  作者: Mango

代码示例1
class Event(object):
    pass

class Observable(object):
    def __init__(self):
        self.callbacks = []
    def subscribe(self, callback):
        self.callbacks.append(callback)
    def fire(self, **attrs):
        e = Event()
        e.source = self
        for k, v in attrs.iteritems():
            setattr(e, k, v)
        for fn in self.callbacks:
            fn(e)