On Sun, Mar 8, 2009 at 11:53 PM, koranthala <koranth...@gmail.com> wrote: > Hi, > I want to implement chain of responsibility pattern in Python (for > a Django project) > The problem that I see is a rather odd one - how to link the > subclasses with the super class.
Grabbing out my copy of Head First Design Patterns and looking up this pattern, it sounds like you're going about this the wrong way. From what I can grok, Chain-of-Responsibility looks like this: class SomeHandler(object) def __init__(self, successor=None): self.successor = successor def theMethod(self, theRequest): result = try_and_handle(theRequest) if did_not_handle(theRequest) and self.successor is not None: return successor.theMethod(theRequest) else: return result #imagine some other handler classes #setup: masterHandler = SomeHandler(OtherHandler(ThirdHandler())) #usage: result = masterHandler.theMethod(aRequest) So the *classes* don't ever know anything about each other; you pass a handler class an instance of its successor handler at construction-time. So there's no need for any registry or introspection at all. You determine the ordering at runtime when you construct the instances. Of course, this entire pattern is usually unnecessary in Python as you can usually write the same thing less verbosely and more straightforwardly as: def someHandler(theRequest): result = try_and_handle(theRequest) if did_not_handle(theRequest): return False, None else: return True, result #imagine some other handler functions #setup: handlers = [someHandler, otherHandler, thirdHandler] #usage: for handler in handlers: success, result = handler(theRequest) if success: break Note the lack of classes and how the sequencing is now made explicit. Cheers, Chris -- I have a blog: http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list