Skip to content

Commit ef809fe

Browse files
committed
Update Mediator pattern
1 parent 6f12a4c commit ef809fe

2 files changed

Lines changed: 31 additions & 125 deletions

File tree

patterns/behavioral/mediator.py

Lines changed: 28 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -2,150 +2,53 @@
22
# -*- coding: utf-8 -*-
33

44
"""
5-
http://web.archive.org/web/20120309135549/http://dpip.testingperspective.com/?p=28
5+
https://www.djangospin.com/design-patterns-python/mediator/
6+
7+
Objects in a system communicate through a Mediator instead of directly with each other.
8+
This reduces the dependencies between communicating objects, thereby reducing coupling.
69
710
*TL;DR80
811
Encapsulates how a set of objects interact.
912
"""
1013

11-
import random
12-
import time
13-
14-
15-
class TC:
16-
def __init__(self):
17-
self._tm = None
18-
self._bProblem = 0
19-
20-
def setup(self):
21-
print("Setting up the Test")
22-
time.sleep(0.1)
23-
self._tm.prepareReporting()
24-
25-
def execute(self):
26-
if not self._bProblem:
27-
print("Executing the test")
28-
time.sleep(0.1)
29-
else:
30-
print("Problem in setup. Test not executed.")
31-
32-
def tearDown(self):
33-
if not self._bProblem:
34-
print("Tearing down")
35-
time.sleep(0.1)
36-
self._tm.publishReport()
37-
else:
38-
print("Test not executed. No tear down required.")
39-
40-
def setTM(self, tm):
41-
self._tm = tm
42-
43-
def setProblem(self, value):
44-
self._bProblem = value
45-
46-
47-
class Reporter:
48-
def __init__(self):
49-
self._tm = None
5014

51-
def prepare(self):
52-
print("Reporter Class is preparing to report the results")
53-
time.sleep(0.1)
15+
class ChatRoom(object):
16+
"""Mediator class"""
5417

55-
def report(self):
56-
print("Reporting the results of Test")
57-
time.sleep(0.1)
18+
def display_message(self, user, message):
19+
print("[{} says]: {}".format(user, message))
5820

59-
def setTM(self, tm):
60-
self._tm = tm
6121

22+
class User(object):
23+
"""A class whose instances want to interact with each other"""
6224

63-
class DB:
64-
def __init__(self):
65-
self._tm = None
25+
def __init__(self, name):
26+
self.name = name
27+
self.chat_room = ChatRoom()
6628

67-
def insert(self):
68-
print("Inserting the execution begin status in the Database")
69-
time.sleep(0.1)
70-
# Following code is to simulate a communication from DB to TC
71-
if random.randrange(1, 4) == 3:
72-
return -1
29+
def say(self, message):
30+
self.chat_room.display_message(self, message)
7331

74-
def update(self):
75-
print("Updating the test results in Database")
76-
time.sleep(0.1)
77-
78-
def setTM(self, tm):
79-
self._tm = tm
80-
81-
82-
class TestManager:
83-
def __init__(self):
84-
self._reporter = None
85-
self._db = None
86-
self._tc = None
87-
88-
def prepareReporting(self):
89-
rvalue = self._db.insert()
90-
if rvalue == -1:
91-
self._tc.setProblem(1)
92-
self._reporter.prepare()
93-
94-
def setReporter(self, reporter):
95-
self._reporter = reporter
96-
97-
def setDB(self, db):
98-
self._db = db
99-
100-
def publishReport(self):
101-
self._db.update()
102-
self._reporter.report()
103-
104-
def setTC(self, tc):
105-
self._tc = tc
32+
def __str__(self):
33+
return self.name
10634

10735

10836
def main():
109-
reporter = Reporter()
110-
db = DB()
111-
tm = TestManager()
112-
tm.setReporter(reporter)
113-
tm.setDB(db)
114-
reporter.setTM(tm)
115-
db.setTM(tm)
116-
# For simplification we are looping on the same test.
117-
# Practically, it could be about various unique test classes and their
118-
# objects
119-
for i in range(3):
120-
tc = TC()
121-
tc.setTM(tm)
122-
tm.setTC(tc)
123-
tc.setup()
124-
tc.execute()
125-
tc.tearDown()
37+
molly = User('Molly')
38+
mark = User('Mark')
39+
ethan = User('Ethan')
40+
41+
molly.say("Hi Team! Meeting at 3 PM today.")
42+
mark.say("Roger that!")
43+
ethan.say("Alright.")
12644

12745

12846
if __name__ == '__main__':
12947
main()
13048

13149

13250
OUTPUT = """
133-
Setting up the Test
134-
Inserting the execution begin status in the Database
135-
Executing the test
136-
Tearing down
137-
Updating the test results in Database
138-
Reporting the results of Test
139-
Setting up the Test
140-
Inserting the execution begin status in the Database
141-
Executing the test
142-
Tearing down
143-
Updating the test results in Database
144-
Reporting the results of Test
145-
Setting up the Test
146-
Inserting the execution begin status in the Database
147-
Executing the test
148-
Tearing down
149-
Updating the test results in Database
150-
Reporting the results of Test
151-
"""
51+
[Molly says]: Hi Team! Meeting at 3 PM today.
52+
[Mark says]: Roger that!
53+
[Ethan says]: Alright.
54+
""" # noqa

tests/test_outputs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from patterns.behavioral.command import OUTPUT as command_output
1919
from patterns.behavioral.iterator import main as iterator_main
2020
from patterns.behavioral.iterator import OUTPUT as iterator_output
21+
from patterns.behavioral.mediator import main as mediator_main
22+
from patterns.behavioral.mediator import OUTPUT as mediator_output
2123
from patterns.behavioral.observer import main as observer_main
2224
from patterns.behavioral.observer import OUTPUT as observer_output
2325
from patterns.behavioral.publish_subscribe import main as publish_subscribe_main
@@ -42,6 +44,7 @@
4244
(chaining_method_main, chaining_method_output),
4345
(command_main, command_output),
4446
(iterator_main, iterator_output),
47+
(mediator_main, mediator_output),
4548
(observer_main, observer_output),
4649
(publish_subscribe_main, publish_subscribe_output),
4750
(specification_main, specification_output),

0 commit comments

Comments
 (0)