I am very familiar with the Command pattern, but I don’t yet understand the difference in theory between a Functor and a command. In particular, I am thinking of Java implementations. Both are basically programming ‘verbs’ represented as objects. However, in the case of functors, as I have seen from some examples anonymous inner class implementations seem common. Can anyone out there clear this up for me nicely?
Share
A functor is a ‘syntax level’ concept – it packages up code in an object that can be treated syntactically like a function pointer – i.e. it can be ‘called’ by putting parameter list in brackets after it. In C++ you could make a class a functor by overriding operator().
A Command in the command pattern is an object that packages up some runnable functionality, but there’s no requirement for it to be a functor. For example, it could be a class that implements an interface ICommand, allowing its command to be run by calling Do().