Alex Rivera | Logout

General Command pattern and Command Dispatch pattern in Python

Asked 2009-09-29T19:23:23.463
27

I was looking for a Command pattern implementation in Python... (According to Wikipedia,

the command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time.

)

The only thing I found was Command Dispatch pattern:

class Dispatcher:

    def do_get(self): ...

    def do_put(self): ...

    def error(self): ...

    def dispatch(self, command):
        mname = 'do_' + command
        if hasattr(self, mname):
            method = getattr(self, mname)
            method()
        else:
            self.error()

May be I'm wrong, but it looks like these are two different concepts, which accidentally have similar names.

Am i missing something?

Edit
Report

1 Answer

4

If I recall the gang of four correctly, the Command pattern is about commands like "File - Save", not commands like "svn commit", which is what your code is good for.

Martin suggests the Command pattern is unneeded because functions as first class objects take its place, but the Command pattern is richer than just doit(), having, for example, also undo(), is_enabled(), etc.

answered 2009-09-29T19:39:20.827

Your Answer