import time
class Observer:
def __init__(self, name) -> None:
self.name = name
def update(self, message):
print("name %s received message: %s" % (self.name, message))
class Subject:
def __init__(self) -> None:
self.objects = []
def add_object(self, observer: Observer):
self.objects.append(observer)
def notify(self, name, message):
for o in self.objects:
if o.name == name:
o.update(message)
actions = ["烟花", "二踢脚", "鱼雷王", "曼城春雨"]
subject = Subject()
for action in actions:
observer = Observer(action)
subject.add_object(observer)
for action in actions:
subject.notify(action, "开始点燃了")
time.sleep(1)
标签:__,name,observer,self,观察者,模式,message,def
From: https://www.cnblogs.com/gatling/p/18041647