]>
Commit | Line | Data |
---|---|---|
e5424f29 MR |
1 | import abc |
2 | ||
3 | class OrmSubject(metaclass=abc.ABCMeta): | |
4 | @abc.abstractmethod | |
5 | def addObserver(self, ormObserver): | |
6 | pass | |
7 | ||
8 | @abc.abstractmethod | |
9 | def removeObserver(self, ormObserver): | |
10 | pass | |
11 | ||
12 | @abc.abstractmethod | |
13 | def notify(self, subject): | |
14 | pass | |
15 | ||
16 | class Observer(metaclass=abc.ABCMeta): | |
17 | @abc.abstractmethod | |
18 | def update(self, subject): | |
19 | pass | |
20 | ||
21 | class BaseOrm(OrmSubject): | |
22 | ||
23 | def __init__(self): | |
24 | self.__observers = [] | |
25 | ||
26 | def addObserver(self, ormObserver): | |
27 | print("Added: {0}".format(ormObserver)) | |
28 | self.__observers.append(ormObserver) | |
29 | ||
30 | def removeObserver(self, ormObserver): | |
31 | self.__observers.remove(ormObserver) | |
32 | ||
33 | def notify(self, object): | |
34 | for observer in self.__observers: | |
35 | observer.update(object) | |
36 | ||
37 | @property | |
38 | def id(self): | |
39 | return self.__id | |
40 | ||
41 | @id.setter | |
42 | def id(self, id): | |
43 | notify = True | |
44 | try: | |
45 | old = self.id | |
46 | notify = False | |
47 | except AttributeError as e: | |
48 | pass | |
49 | # print(e) | |
50 | # notify = False | |
51 | self.__id = id | |
52 | if notify: | |
53 | self.notify(self) |