Command Design Pattern in Java

Command Pattern

The command pattern comes under behavioral design pattern. The Command design pattern is used to create objects that represent actions and events in an application. In the command pattern, a command interface declares a method for executing a particular action. A command object encapsulates an action or event and contains all information required to understand the action or event. This command object can be used as a parameter to extract information about occurred actions and events.

In the command pattern, the invoker is decoupled from the action performed by the receiver. The invoker invokes a command that executes the appropriate action of the receiver. Hence invoker is unaware of details of the action to be performed.

The command pattern can be used to perform redo/undo functionality.

Class Diagram

The class diagram for the command design pattern is depicted below:

Command Pattern in Java

Command Pattern Implementation

Create an abstraction for the command design.

public interface Commandable {

	void execute();

}

The following class defines methods for operations which are to be performed based on the given command.

public class CutPaste {

	public void redo() {
		System.out.println("Cut/Paste the selected text");
	}

	public void undo() {
		System.out.println("Revert Cut/Paste");
	}

}

Concrete classes are used to provide the services required by the particular command.

The following class will redo the operation.

public class RedoCutPasteCommand implements Commandable {

	private CutPaste cutPaste;

	public RedoCutPasteCommand(CutPaste cutPaste) {
		this.cutPaste = cutPaste;
	}

	@Override
	public void execute() {
		cutPaste.redo();
	}

}

The following class will undo the operation:

public class UndoCutPasteCommand implements Commandable {

	private CutPaste cutPaste;

	public UndoCutPasteCommand(CutPaste cutPaste) {
		this.cutPaste = cutPaste;
	}

	@Override
	public void execute() {
		cutPaste.undo();
	}

}

Create invoker class which is a reference to the Command interface to invoke the command.

public class CommandInvoker {

	private Commandable command;

	public CommandInvoker(Commandable command) {
		this.command = command;
	}

	public void invoke() {
		command.execute();
	}

}

Create a main class to test the command design pattern:

public class CommandPatternTest {
	
	public static void main(String[] args) {
		CutPaste cutPaste = new CutPaste();
		Commandable redoCutPasteCommand = new RedoCutPasteCommand(cutPaste);
		Commandable undoCutPasteCommand = new UndoCutPasteCommand(cutPaste);
		
		CommandInvoker commandInvoker = new CommandInvoker(undoCutPasteCommand);
		commandInvoker.invoke();
		
		commandInvoker = new CommandInvoker(redoCutPasteCommand);
		commandInvoker.invoke();
	}
	
}

Run the above main class and you will see the following output:

Revert Cut/Paste
Cut/Paste the selected text

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *