-
Notifications
You must be signed in to change notification settings - Fork 3
/
mvc.py
37 lines (26 loc) · 995 Bytes
/
mvc.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
"""
Base components for connecting model to view.
"""
class MVCEvent(object):
"""Abstract base class for events"""
def __init__(self, subject: any) -> None:
"""The 'subject' is the object announcing the event"""
self.subject = subject
self.addr = None
self.value = None
class MVCListener(object):
"""Abstract base class.
Extend this and override the notify method.
"""
def notify(self, mvc_event) -> None:
"""Override this method in listeners"""
raise NotImplementedError("The notify method should be overridden in {}".format(self.__class__))
class MVCListenable(object):
"""A model object that a view object can listen to"""
def __init__(self):
self.listeners = []
def register_listener(self, listener: MVCListener) -> None:
self.listeners.append(listener)
def notify_all(self, event: MVCEvent) -> None:
for listener in self.listeners:
listener.notify(event)