Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Functor and the Command pattern?

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?

like image 864
Elijah Avatar asked Nov 03 '08 11:11

Elijah


People also ask

What is the difference between a function and a functor?

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.

What is functor?

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.

What does command pattern do?

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.

What is functor in Java?

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.


2 Answers

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'.

like image 114
Martin York Avatar answered Oct 03 '22 03:10

Martin York


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

like image 40
mackenir Avatar answered Oct 03 '22 05:10

mackenir