Question

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?

Was it helpful?

Solution

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().

OTHER TIPS

A functor is an implementation, a way of making an object behave like a function.

The 'Command Pattern' is a design pattern.
The functor is one way to implement the 'Command Pattern'.

Here comes the answer from the GOF:

Coplien describes how to implement functors, objects that are functions, in C++ [Cop92]. He achieves a degree of transparency in their use by overloading the function call operator (operator()). The Command pattern is different; its focus is on maintaining a binding between a receiver and a function (i.e., action), not just maintaining a function.

From the description of the Apache Commons Functor page:


A functor is a function that can be manipulated as an object, or an object representing a single, generic function.

Functors support and encourage a number of powerful programming techniques including:

  • programming in a functional style
  • higher order functions
  • internal iterators
  • reuse and specialization through composition rather than inheritance and overloading
  • generic "callback" or "extension point" APIs
  • generic "filters" or predicate APIs
  • many "behavioral" design patterns, such as Visitor, Strategy, Chain of Responsibility, etc.

According to GOF (Gang of Four) cited definition (by Comptrol), Functor and Command are two different Patterns.

As already stated, Functor represents a Class containing a service method, in other terms: the main responsibility for the functor class is to store the specific logic implemented in its own method. Hence, we can think at the functor how a container for its own method's inner logic. Historically, Functor borns because in the Java specification is not present the implementation and/or concept of 'function pointer', which is very useful in the context of registered callbacks (specific implementation for the observer pattern).

The Command Pattern represents a Design pattern aimed to decouple an Invoker entity from a Receiver one. It is mainly used when it is needed to decuple the actions (generating the events) from the action listeners (think to GUIs). It has a method responsible for the excecution of a specific operation (depending on the particular command implementation from its own superclass) referring to a specific Object Receiver; in the stated definition, the execution method can be defined as "not smart", in fact with a smart implementation could be itself to implement operative logic instead of delegating it to a third object. When we have a smart execution method, we are implementing a functor, and we are putting the functor (specific command subpart implementation) in the context of command pattern.

I hope this will be helpful for you.

I think of a functor as being a component of the command pattern, which also involves other infrastructure such as the invoker and command recipients.

The Command pattern is used in Java because we don't have closures in Java. Functors is an attempt to implement closures.

A language with closures doesn't need the Command pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top