Search code examples
design-patternscommand-pattern

Is an anonymous function a good way to implement the command pattern?


I'm quite new to this pattern...


Solution

  • If you've got lambdas, you don't really need half of "design" "patterns" altogether.

    Factory? That's just a function returning new objects. Visitor? Duh! Command? Anonymous function. Interpreter? Function which takes string or whatever. Strategy? It's a function!

    That's just lambdas, functions and closures.

    The problem is, about 1/3 to 1/2 of them were basically cover-ups for deficiencies in C++ that don't exist in other languages. Although I'm not a huge Perl fan anymore, I have to admit the Perl community caught on to this first (or at least funniest). They pointed out that many of these so-called patterns were actually an implementation of Functional Programming in C++.

    So yes, you can use anonymous (or otherwise named) functions where you would use the Command pattern.