-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobserver.py
68 lines (44 loc) · 1.42 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
from __future__ import annotations
import string
import threading
from abc import ABC, abstractmethod
from typing import List
import speech_generator
class Subject(ABC):
@abstractmethod
def attach(self, observer: Observer) -> None:
pass
@abstractmethod
def detach(self, observer: Observer) -> None:
pass
@abstractmethod
def notify(self) -> None:
pass
class Person(Subject):
_name: string = "Unbekannt"
_has_mask: bool = True
_observers: List[Observer] = []
def attach(self, observer: Observer) -> None:
self._observers.append(observer)
def detach(self, observer: Observer) -> None:
self._observers.remove(observer)
def notify(self) -> None:
for observer in self._observers:
observer.update(self)
def update_name(self, newname) -> None:
if newname != self._name:
self._name = newname
self.notify()
def update_mask(self, status) -> None:
if status != self._has_mask:
self._has_mask = status
self.notify()
class Observer(ABC):
@abstractmethod
def update(self, subject: Subject) -> None:
pass
class MaskObserver(Observer):
def update(self, subject: Subject) -> None:
if subject._has_mask is False:
t = threading.Thread(target=speech_generator.generate_output_speech, args=[subject._name])
t.start()