0

Possible Duplicate:
Help with understanding a function object or functor in Java

I am currently reading the book "data structures and problem solving using Java" by Mark Allen Weiss and the explanation of a functor is not clear to me.

1) Why do you use them? 2) Exactly what do they do?

The only thing I am getting from this book is if a particular object does not have a compareTo method you use a function object to give it one ha-ha.....help me!!

Community
  • 1
  • 1
Kristopher
  • 205
  • 2
  • 3
  • 7

2 Answers2

2

Functors encapsulate functions as objects. You can pass them around to other objects to get things done.

Think Gang of Four Command Pattern.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

If an object does not implement Comparable you can write an implementation of Comparator to achieve the same effect. Comparator is, in fact, the example given in the wikipedia arctile about functors in Java. If the an object is not Comparable it can still be sorted by Collections.sort(Collection, Comparator) by providing a relevant Comparator implementation.

The interface Comparator is an example of a functor. Implementations of it can be passed around for other objects or methods to use to do something (In this case compare two objects).

Dev
  • 11,919
  • 3
  • 40
  • 53