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?
Asked by: Lyndon797 | Posted: 23-01-2022
Answer 1
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'.
Answer 2
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().
Answered by: First Name800 | Posted: 24-02-2022Answer 3
Here comes the answer from the GOF:
Answered by: Byron779 | Posted: 24-02-2022Coplien 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.
Answer 4
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.
Answer 5
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.
Answered by: Daisy933 | Posted: 24-02-2022Answer 6
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.
Answered by: Lenny673 | Posted: 24-02-2022Answer 7
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.
Answered by: Nicole633 | Posted: 24-02-2022Similar questions
What's the difference between JavaScript and Java?
java - Difference between value and itemvalue
What is the difference between the value and itemValue attribute of the radiobutton in Jsf?
java - Difference between Oracle ATG and Struts?
Closed. This question needs to be more focused. It ...
syntax - Is there a difference between single and double quotes in Java?
Is there a difference between single and double quotes in Java?
java - What is the difference between Swing and AWT?
Can someone please explain me what's the difference between Swing and AWT?
Are there any cases where AWT is more useful/advised to use than swing or vice-versa?
java - Difference between declaring variables before or in loop?
I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference?
A (quite pointless) example in Java:
a) declaration before loop:
double intermediateResult;
for(int i=0; i < 1000; i++){
intermediateResult = i;
System.out.println(intermediateResult);
}
...
java - Difference between break and continue statement
Can anyone tell me the difference between break and continue statements?
Difference between '.' and "." in java
Is there a difference between concatenating strings with '' and ""?
For example, what is the difference between:
String s = "hello" + "/" + "world";
and
String s = "hello" + '/' + "world";
java - Difference between moving an Iterator forward with a for statement and a while statement
When I use an Iterator of Object I use a while loop (as written in every book learning Java, as Thinking in Java of Bruce Eckel):
Iterator it=...
while(it.hasNext()){
//...
}
but sometime i saw than instead somebody use the for loop:
Iterator it=...
for (Iterator it=...; it.hasNext();){
//...
}
I don't' understan...
java - Difference between Set and Collection in hibernate
What is the difference between using a Set or a Collection for @OneToMany or @ManyToMany properties on my hibernate entity objects?
Does Hibernate map things differently depending on which one you choose?
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)