Javatpoint Logo
Javatpoint Logo

Command Pattern

A Command Pattern says that "encapsulate a request under an object as a command and pass it to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command".

It is also known as Action or Transaction.


Advantage of command pattern

  • It separates the object that invokes the operation from the object that actually performs the operation.
  • It makes easy to add new commands, because existing classes remain unchanged.

Usage of command pattern:

It is used:

  • When you need parameterize objects according to an action perform.
  • When you need to create and execute requests at different times.
  • When you need to support rollback, logging or transaction functionality.

Example of command pattern

Let's understand the example of adapter design pattern by the above UML diagram.

UML for command pattern:

These are the following participants of the Command Design pattern:
  • Command This is an interface for executing an operation.
  • ConcreteCommand This class extends the Command interface and implements the execute method. This class creates a binding between the action and the receiver.
  • Client This class creates the ConcreteCommand class and associates it with the receiver.
  • Invoker This class asks the command to carry out the request.
  • Receiver This class knows to perform the operation.
command pattern UML

Implementation of above UML:

Step 1

Create a ActionListernerCommand interface that will act as a Command.

Step 2

Create a Document class that will act as a Receiver.

File: Document.java

Step 3

Create a ActionOpen class that will act as an ConcreteCommand.

File: ActionOpen.java

Step 4

Create a ActionSave class that will act as an ConcreteCommand.

File: AdapterPatternDemo.java

Step 5

Create a MenuOptions class that will act as an Invoker.

File: ActionSave.java

Step 6

Create a CommanPatternClient class that will act as a Client.

File: AdapterPatternDemo.java

Output





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials