Monday, December 04, 2006

Implementing Command Pattern in Java

The previous post described the Command pattern in brief. I listed out where and why the command pattern may be used. This post describes how to implement command pattern in Java and also some implementation considerations. The following is the UML diagram for command patternThe following is a simple description of each of the elements of the above diagram, followed by a simple implementation.

Client: The client is responsible for creating the Command object and setting it's reciever.

Invoker: The Invoker acts as a placeholder for the Command object and invokes the execute method on the Command. In case of undoable commands, it stores the command in a stack (for multi-level undo, or just the command for single level undo), before executing the command.

Receiver: The object that performs the operations associated with carrying out a request. Any class may serve as a Receiver.

public class Receiver { private int value;

public Receiver() { value = 0; }

public void increment() { ++value;

}

public void decrement() { --value; }

public int getValue() { return value; }

}

Receiver.java

Command: The command object represents the request operation. The command implements execute() method, which invokes the corresponding operations on the Reciever. This defines a binding between a Receiver object and an action.

public interface Command { public void execute(); public void undo();}

A command can have a wide range of abilities from a simple interface between the client and receiver to being a receiver itself.

When supporting multi-level undo, a command may store state information, which mean that, with each execute(), you have to copy the state of the command at that time. In such cases a copy of the command has to be added to the history stack.