```python class Observer: def update(self, subject): pass
class ConcreteObserver(Observer): def update(self, subject): print(f"Observer: Received update from {subject.getName()}")
class Subject: def init(self): self._observers = []
def attach(self, observer):
self._observers.append(observer)
def detach(self, observer):
self._observers.remove(observer)
def notify(self):
for observer in self._observers:
observer.update(self)
复制代码
class ConcreteSubject(Subject): def init(self, name): super(ConcreteSubject, self).init() self._name = name