Command Design Pattern in Java

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


Create an interface

Create a class called CutPaste

Create concrete classes

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

Create a test class to test the Command pattern

Run the above test class and see the below output


That’s all. Thanks for your reading.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.