Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 28 additions & 125 deletions patterns/behavioral/mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,150 +2,53 @@
# -*- 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__':
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
3 changes: 3 additions & 0 deletions tests/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down