|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 | """ |
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. |
6 | 9 |
|
7 | 10 | *TL;DR80 |
8 | 11 | Encapsulates how a set of objects interact. |
9 | 12 | """ |
10 | 13 |
|
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 |
50 | 14 |
|
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""" |
54 | 17 |
|
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)) |
58 | 20 |
|
59 | | - def setTM(self, tm): |
60 | | - self._tm = tm |
61 | 21 |
|
| 22 | +class User(object): |
| 23 | + """A class whose instances want to interact with each other""" |
62 | 24 |
|
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() |
66 | 28 |
|
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) |
73 | 31 |
|
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 |
106 | 34 |
|
107 | 35 |
|
108 | 36 | 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.") |
126 | 44 |
|
127 | 45 |
|
128 | 46 | if __name__ == '__main__': |
129 | 47 | main() |
130 | 48 |
|
131 | 49 |
|
132 | 50 | 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 |
0 commit comments