-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.py
112 lines (84 loc) · 3.1 KB
/
observer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""观察者模式"""
from __future__ import annotations
from abc import ABC, abstractmethod
from random import randrange
from typing import List
class Subject(ABC):
"""The Subject interface declared a set of methods for managing subscribers."""
@abstractmethod
def attach(self, observer: Observer) -> None:
"""Attach an observer to the subject."""
pass
@abstractmethod
def detach(self, observer: Observer) -> None:
"""Detach an observer from the subject."""
pass
@abstractmethod
def notify(self) -> None:
"""Notify all observers about an event"""
pass
class Observer(ABC):
"""The Observer interface declares the update method, used by the subject."""
@abstractmethod
def update(self, subject: Subject) -> None:
pass
class ConcreteSubject(Subject):
"""
The Subject owns some import state and notifies observers when the state changes.
"""
state: int = None
"""
For the sake of simplicity, the Subject's state, essential to all subscribers,
is stored in this variable.
"""
_observers: List[Observer] = []
"""
List of subscribers. In real life, the list of subscribers can be stored more
comprehensively(categorized by event type, etc.).
"""
def attach(self, observer: Observer) -> None:
print("\nSubject: attach observer to the subject")
self._observers.append(observer)
def detach(self, observer: Observer) -> None:
print("\nSubject: detach observer to the subject")
self._observers.remove(observer)
"""
The subscription management methods.
"""
def notify(self) -> None:
"""
Trigger an update in each subscriber.
"""
print("\nSubject: Notifying observers...")
for observer in self._observers:
observer.update(self)
def do_some_business_logic(self) -> None:
"""
Usually, the subscription logic is only a fraction of what a Subject
can really do. Subjects commonly hold some import business logic,
that triggers a notification method whenever something import is about
to do(or after it).
"""
print("\nSubject: I'm doing something important.")
self.state = randrange(0, 10)
print(f"Subject: My state has just changed to: {self.state}")
self.notify()
# concrete Observers react to the updates issued by the Subject they had been attached to.
class ConcreteObserverA(Observer):
def update(self, subject: ConcreteSubject) -> None:
if subject.state < 8:
print("\nConcreteObserverA: react to the event")
class ConcreteObserverB(Observer):
def update(self, subject: ConcreteSubject) -> None:
if subject.state > 4:
print("\nConcreteObserverB: react to the event")
if __name__ == "__main__":
sbj = ConcreteSubject()
observerA = ConcreteObserverA()
sbj.attach(observerA)
observerB = ConcreteObserverB()
sbj.attach(observerB)
sbj.do_some_business_logic()
sbj.do_some_business_logic()
sbj.detach(observerB)
sbj.do_some_business_logic()