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!!

0

2 Answers 2

2

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

Think Gang of Four Command Pattern.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.