Skip to content

Instantly share code, notes, and snippets.

@brent-hoover
Created June 15, 2011 19:39
Show Gist options
  • Select an option

  • Save brent-hoover/1027908 to your computer and use it in GitHub Desktop.

Select an option

Save brent-hoover/1027908 to your computer and use it in GitHub Desktop.
The Master Mediator
#!/usr/bin/env python
#coding:utf-8
# created on 6/15/11
from django.dispatch.dispatcher import receiver, Signal
from skumanage.dataobjects import Action
from skumanage.models import Product, ProductImage
import abc
class MediatorBase(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def handle(self, event):
"""Retrieve data from the input source
and return an object.
"""
class AshepSignal(Signal):
"""
Generic signal that we always use to fire signals, so that one listener can listen.
"""
def __init__(self):
self.event = None
def fire(self, event):
#fire signal with event as arguments
# actual code goes here
# event = verb, noun, group
pass
class ProductMediator(MediatorBase):
"""
Fake class for now just to show inheritance
"""
handles = [Product, ProductImage]
def handle(self, event):
pass
@receiver(AshepSignal)
def master_mediator(sender, event, **kwargs):
"""
All signals go here first, and are then passed off to one or many mediator subclasses
that handle that object.
"""
for sc in MediatorBase.__subclasses__():
if event.object in sc.handles:
sc.handle(event)
@brent-hoover
Copy link
Author

This decouple all signal/events from handlers so that they don't need to be wired directly together or even know who handles them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment