Created
June 15, 2011 19:39
-
-
Save brent-hoover/1027908 to your computer and use it in GitHub Desktop.
The Master Mediator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This decouple all signal/events from handlers so that they don't need to be wired directly together or even know who handles them.