/ Design Patterns / Behavioral patterns / Observer Observer Design Pattern in Python Back to Observer description """ Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updatedautomatically. """ import abc class Subject: """ Know its observers. Any number of Observer objects may observe a subject. Send a notification to its observers when its state changes. """ def __init__(self): self._observers = set() self._subject_state = None def attach(self, observer): observer._subject = self self._observers.add(observer) def detach(self, observer): observer._subject = None self._observers.discard(observer) def _notify(self): for observer in self._observers: observer.update(self._subject_state) @property def subject_state(self): return self._subject_state @subject_state.setter def subject_state(self, arg): self._subject_state = arg self._notify() class Observer(metaclass=abc.ABCMeta): """ Define an updating interface for objects that should be notified of changes in a subject. """ def __init__(self): self._subject = None self._observer_state = None @abc.abstractmethod def update(self, arg): pass class ConcreteObserver(Observer): """ Implement the Observer updating interface to keep its state consistent with the subject's. Store state that should stay consistent with the subject's. """ def update(self, arg): self._observer_state = arg # ... def main(): subject = Subject() concrete_observer = ConcreteObserver() subject.attach(concrete_observer) subject.subject_state = 123 if __name__ == "__main__": main() Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...