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?
A function assigns to every element of a set X an element of a set Y. A functor assigns to every object of a category C an object of a category D and also assigns to every morphism in C a morphism in D in a way compatible with sources, targets, and composition.
In functional programming, a functor is a design pattern inspired by the definition from category theory, that allows for a generic type to apply a function inside without changing the structure of the generic type. Simple examples of this are Option and collection types.
In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
Commons Functor: Function Objects for Java 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.
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'.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With