-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathObserver.py
More file actions
45 lines (32 loc) · 961 Bytes
/
Observer.py
File metadata and controls
45 lines (32 loc) · 961 Bytes
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
class JobPost:
_title = None
def __init__(self, title):
self.title = title
def getTitle(self):
return self.title
class JobSeeker:
_name = None
def __init__(self, name):
self.name = name
def onJobPosted(self, job):
print 'Hi ' + self.name + '! New job posted: ' + job.getTitle()
class EmploymentAgency:
_observers = []
def notify(self, jobPosting):
for observer in self._observers:
observer.onJobPosted(jobPosting)
def attach(self, observer):
self._observers.append(observer)
def addJob(self, jobPosting):
self.notify(jobPosting)
johnDoe = JobSeeker('John Doe')
janeDoe = JobSeeker('Jane Doe')
jobPostings = EmploymentAgency()
jobPostings.attach(janeDoe)
jobPostings.attach(johnDoe)
jobPostings.addJob(JobPost('Software Engineer'))
'''
Output
Hi John Doe! New job posted: Software Engineer
Hi Jane Doe! New job posted: Software Engineer
'''