faif / python-patterns

A collection of design patterns/idioms in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mediator pattern is overcomplicated

gyermolenko opened this issue · comments

Looking through mediator pattern example I find it overcomplicated.

What I could get from it, in short:

  • It has TestManager class in a role of Mediator. It manages TestCase, Reporting, DB interactions
  • during TestCase setup - DB insert operation is executed. Result is randomized and can end successfully or result in error
  • if DB insert fails - special TestCase attribute problem is set

I find it hard to follow and would like to hear opinion of another person.

To complare, I like the following example much more
https://www.djangospin.com/design-patterns-python/mediator/

class ChatRoom(object):
    '''Mediator class.'''
    def displayMessage(self, user, message):
        print("[{} says]: {}".format(user, message))
 
class User(object):
    '''A class whose instances want to interact with each other.'''
    def __init__(self, name):
        self.name = name
        self.chatRoom = ChatRoom()
 
    def sendMessage(self, message):
        self.chatRoom.displayMessage(self, message)
 
    def __str__(self):
        return self.name
 
molly = User('Molly')
mark = User('Mark')
ethan = User('Ethan')
 
molly.sendMessage("Hi Team! Meeting at 3 PM today.")
mark.sendMessage("Roger that!")
ethan.sendMessage("Alright.")
 
### OUTPUT ###
[Molly says]: Hi Team! Meeting at 3 PM today.
[Mark says]: Roger that!
[Ethan says]: Alright.

Good suggestion. Go ahead, but please replace camel case method names with more Pythonic names (e.g. display_message)