The command pattern comes under behavioral design pattern. The Command 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

Command Pattern in Java

Implementation

Create an interface

package com.roytuts.designpattern.command;

public interface Command {

  void execute();

}

Create a class called CutPaste

package com.roytuts.designpattern.command;

public class CutPaste {

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

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

}

Create concrete classes

package com.roytuts.designpattern.command;

public class UndoCutPasteCommand implements Command {

  private CutPaste cutPaste;

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

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

}
package com.roytuts.designpattern.command;

public class RedoCutPasteCommand implements Command {

  private CutPaste cutPaste;

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

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

}

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

package com.roytuts.designpattern.command;

public class CommandInvoker {

  private Command command;

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

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

Create a test class to test the Command pattern

package com.roytuts.designpattern.command;

public class CommandPatternTest {

  /**
   * @param args
   */
  public static void main(String[] args) {

    CutPaste cutPaste = new CutPaste();
    Command redoCutPasteCommand = new RedoCutPasteCommand(cutPaste);
    Command undoCutPasteCommand = new UndoCutPasteCommand(cutPaste);

    CommandInvoker commandInvoker = new CommandInvoker(undoCutPasteCommand);
    commandInvoker.invoke();

    commandInvoker = new CommandInvoker(redoCutPasteCommand);
    commandInvoker.invoke();

  }

}

Run the above test class and see the below output

Output 

Revert Cut/Paste
Cut/Paste the selected text

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

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