Fork me on GitHub

The Functor Pattern


Provides a distributed implementation of the classic Function Objects (Wikipedia) or as it is also known, the Functor Pattern, built using Oracle Coherence.


The Functor Pattern implementation is distributed in the jar file called: coherence-functorpattern-11.2.0.jar.

However as the Functor Pattern has several other dependencies, we **strongly recommend** that developers adopt and use tools like Apache Maven/Ivy/Gradle to transitively resolve the said dependencies, instead of attempting to do so manually.

To configure your Apache Maven-based application to use the Functor Pattern, simply add the following declaration to your application pom.xml file <dependencies> element.



The Functor Pattern is an extension to the Command Pattern. Naturally this means the use and semantics of the Functor Pattern are mostly identical to those of the Command Pattern, the only difference being that the Functor Pattern provides the ability to receive returned values (or re-throw exceptions) from submitted requests.

The mechanism to acquire return values, any exceptions and/or status of execution is provided through the use of Java 5+ Futures.

It is highly recommended that you understand the use of the Command Pattern before attempting to use the Functor Pattern.

As the Functor Pattern provides the ability to acquire return values it's a useful alternative to Oracle Coherence EntryProcessors with the advantage that Functors are executed asynchronously, instead of synchronously as is the case with EntryProcessors.


Let's start with a simple example where we use the Functor Pattern to asynchronously increment and return the value of a Counter.

First, we'll start by writing a simple Counter.


public class Counter implements Context, Serializable {
  private long next;
  public Counter(long initialValue) { = initialValue;

  public long next() {
    return next++;

  public String toString() {
    return String.format("Counter{next=%d}", next);

Second, let's write a Functor that will increment the value of a .Counter.


public class NextValueFunctor implements Functor<Counter, Long>, Serializable {
  private static final long serialVersionUID = 4841498972676821911L;

  public Long execute(ExecutionEnvironment<Counter> executionEnvironment) {
    Counter counter = executionEnvironment.getContext();        
    long next =;
    return next;

Lastly, let's write a simple example to test our Functor.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class FunctorPatternExample {
  public static void main(String[] args) throws InterruptedException, ExecutionException {

    ContextsManager contextsManager = DefaultContextsManager.getInstance();
    Identifier contextIdentifier = contextsManager.registerContext("myCounter", new Counter(0));

    FunctorSubmitter functorSubmitter = DefaultFunctorSubmitter.getInstance();
    functorSubmitter.submitCommand(contextIdentifier, new LoggingCommand("Commenced", 0));

    for (int i = 0; i < 50; i++) {
      Future<Long> future = functorSubmitter.submitFunctor(contextIdentifier, new NextValueFunctor());

    functorSubmitter.submitCommand(contextIdentifier, new LoggingCommand("Completed", 0));


    Counter counter = (Counter)contextsManager.getContext(contextIdentifier);

References and Additional Information