tags:

views:

47

answers:

1

What is the use of the Transformer function in the apache commons collection ?

How do I use it in programs and at what times?

+1  A: 

Transformers are function objects (also referred abusively as "software" functors, even though functor are a mathematical notion from the Category Theory).
They represent a function encapsulated in an object.

In most cases, the function’s parameters can be set and the result retrieved using the common accessors pattern, such as setParameter1(Object value) or getResult().

The fact that these functions are encapsulated by real objects is also the reason for its greatest benefit: the use of many design patterns including structural ones such as the Decorator pattern and behavioral ones such as the Visitor pattern.

So you use Transformers to apply a pattern on Collections, but also on other objects.

Here is an example of a creational pattern used through a Transformer.

VonC
This use of "functor" is incorrect, although widely held. A functor is actually a type-level transformation. See http://mathworld.wolfram.com/Functor.html
Apocalisp
@Apocalisp: Agreed. I have corrected the answer to reflect the distinction between functor and "function object"
VonC