示例:

    1. from common import Observer, Observable
    2. class Boss(Observer):
    3. def __init__(self, name):
    4. self.name = name
    5. def update(self, observable, *args, *kwargs):
    6. print("%s recive %s work report: %s" % (self.name, args[1], args[2]))
    7. class Employee(Observable):
    8. def __init__(self, name):
    9. self.name = name
    10. def submit_work_report(self, report):
    11. self.notifyObservers(self, self.name, report)
    12. Henry = Boss("Henry")
    13. Tony = Employee("Tony")
    14. Jimmy = Employee("Jimmy")
    15. Tony.addObserver(Henry)
    16. Jimmy.addObserver(Henry)
    17. Tony.submit_work_report("2000-01-01 work over")
    18. # Henry recive Tony work report: 2000-01-01 work over
    19. Jimmy.submit_work_report("2000-01-01 work deferral")
    20. # Henry recive Jimmy work report: 2000-01-01 work deferral
    21. Jimmy.delObserver(Henry)
    22. Leaf = Boss("Leaf")
    23. Jimmy.addObserver(Leaf)
    24. Jimmy.submit_work_report("2001-01-01 work over")
    25. # Leaf recive Jimmy work report: 2001-01-01 work over