From ef809fef611ec5703d46254744047321104ff62f Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Thu, 7 Feb 2019 15:11:27 +0200 Subject: [PATCH] Update Mediator pattern --- patterns/behavioral/mediator.py | 153 ++++++-------------------------- tests/test_outputs.py | 3 + 2 files changed, 31 insertions(+), 125 deletions(-) diff --git a/patterns/behavioral/mediator.py b/patterns/behavioral/mediator.py index db23b7bba..9aee4a180 100644 --- a/patterns/behavioral/mediator.py +++ b/patterns/behavioral/mediator.py @@ -2,127 +2,45 @@ # -*- coding: utf-8 -*- """ -http://web.archive.org/web/20120309135549/http://dpip.testingperspective.com/?p=28 +https://www.djangospin.com/design-patterns-python/mediator/ + +Objects in a system communicate through a Mediator instead of directly with each other. +This reduces the dependencies between communicating objects, thereby reducing coupling. *TL;DR80 Encapsulates how a set of objects interact. """ -import random -import time - - -class TC: - def __init__(self): - self._tm = None - self._bProblem = 0 - - def setup(self): - print("Setting up the Test") - time.sleep(0.1) - self._tm.prepareReporting() - - def execute(self): - if not self._bProblem: - print("Executing the test") - time.sleep(0.1) - else: - print("Problem in setup. Test not executed.") - - def tearDown(self): - if not self._bProblem: - print("Tearing down") - time.sleep(0.1) - self._tm.publishReport() - else: - print("Test not executed. No tear down required.") - - def setTM(self, tm): - self._tm = tm - - def setProblem(self, value): - self._bProblem = value - - -class Reporter: - def __init__(self): - self._tm = None - def prepare(self): - print("Reporter Class is preparing to report the results") - time.sleep(0.1) +class ChatRoom(object): + """Mediator class""" - def report(self): - print("Reporting the results of Test") - time.sleep(0.1) + def display_message(self, user, message): + print("[{} says]: {}".format(user, message)) - def setTM(self, tm): - self._tm = tm +class User(object): + """A class whose instances want to interact with each other""" -class DB: - def __init__(self): - self._tm = None + def __init__(self, name): + self.name = name + self.chat_room = ChatRoom() - def insert(self): - print("Inserting the execution begin status in the Database") - time.sleep(0.1) - # Following code is to simulate a communication from DB to TC - if random.randrange(1, 4) == 3: - return -1 + def say(self, message): + self.chat_room.display_message(self, message) - def update(self): - print("Updating the test results in Database") - time.sleep(0.1) - - def setTM(self, tm): - self._tm = tm - - -class TestManager: - def __init__(self): - self._reporter = None - self._db = None - self._tc = None - - def prepareReporting(self): - rvalue = self._db.insert() - if rvalue == -1: - self._tc.setProblem(1) - self._reporter.prepare() - - def setReporter(self, reporter): - self._reporter = reporter - - def setDB(self, db): - self._db = db - - def publishReport(self): - self._db.update() - self._reporter.report() - - def setTC(self, tc): - self._tc = tc + def __str__(self): + return self.name def main(): - reporter = Reporter() - db = DB() - tm = TestManager() - tm.setReporter(reporter) - tm.setDB(db) - reporter.setTM(tm) - db.setTM(tm) - # For simplification we are looping on the same test. - # Practically, it could be about various unique test classes and their - # objects - for i in range(3): - tc = TC() - tc.setTM(tm) - tm.setTC(tc) - tc.setup() - tc.execute() - tc.tearDown() + molly = User('Molly') + mark = User('Mark') + ethan = User('Ethan') + + molly.say("Hi Team! Meeting at 3 PM today.") + mark.say("Roger that!") + ethan.say("Alright.") if __name__ == '__main__': @@ -130,22 +48,7 @@ def main(): OUTPUT = """ -Setting up the Test -Inserting the execution begin status in the Database -Executing the test -Tearing down -Updating the test results in Database -Reporting the results of Test -Setting up the Test -Inserting the execution begin status in the Database -Executing the test -Tearing down -Updating the test results in Database -Reporting the results of Test -Setting up the Test -Inserting the execution begin status in the Database -Executing the test -Tearing down -Updating the test results in Database -Reporting the results of Test -""" +[Molly says]: Hi Team! Meeting at 3 PM today. +[Mark says]: Roger that! +[Ethan says]: Alright. +""" # noqa diff --git a/tests/test_outputs.py b/tests/test_outputs.py index d68ea51d6..07334491b 100644 --- a/tests/test_outputs.py +++ b/tests/test_outputs.py @@ -18,6 +18,8 @@ from patterns.behavioral.command import OUTPUT as command_output from patterns.behavioral.iterator import main as iterator_main from patterns.behavioral.iterator import OUTPUT as iterator_output +from patterns.behavioral.mediator import main as mediator_main +from patterns.behavioral.mediator import OUTPUT as mediator_output from patterns.behavioral.observer import main as observer_main from patterns.behavioral.observer import OUTPUT as observer_output from patterns.behavioral.publish_subscribe import main as publish_subscribe_main @@ -42,6 +44,7 @@ (chaining_method_main, chaining_method_output), (command_main, command_output), (iterator_main, iterator_output), + (mediator_main, mediator_output), (observer_main, observer_output), (publish_subscribe_main, publish_subscribe_output), (specification_main, specification_output),