• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Command Design Pattern

June 22, 2017 By j2eereference 1 Comment

Command Design Pattern : Command design pattern comes under behavioral design pattern. Command design patter is used to implement request-response model.

To understand command pattern in a better way, let’s understand key components first:

Command: A command is an interface which declares execute() method to execute a specific action.

Concrete Command : This class is an implementation of Command interface which provides actual implementation of execute() method.

Invoker : An invoker class contains a reference of  a command and  instructs the command to perform an action.

Receiver : A Receiver is a class which provides logic to perform actions asked by command.Execute() method of concrete command class invokes action method of receiver class.

Client : Client class will create instances of Receiver, Invoker and concrete command and then associates  ConcreteCommand with Receiver class to perform action.

In command design pattern, client class creates instance of concrete command and set receiver object in concrete command. After that it creates instance of Invoker class and set command object in it and then invoker class forwards request from client to command object .Invoker has no knowledge about action performed by receiver class it just invokes command which then executes the action of receiver class.

Client Class: CommandDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.j2eereference.designPattern;
 
public class CommandDemo {
 
public static void main(String args[])
{
  //Receiver Class
  AdditionReceiver additionReceiver  = new AdditionReceiver();
  //Concrete command class
  Command addCommand = new AdditionCommand(additionReceiver);
  //Invoker class
  ActionInvoker actionInvoker = new ActionInvoker(addCommand);
  actionInvoker.invokeMethod();
}
}

Invoker class: ActionInvoker.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.j2eereference.designPattern;
 
public class ActionInvoker
{
  Command command;
  public ActionInvoker(Command command)
  {
    this.command = command;
  }
  public void setCommand(Command command)
  {
    this.command = command;
  }
  public void invokeMethod()
  {
    command.execute();
  }
}

Command Interface: Command.java

Java
1
2
3
4
5
package com.j2eereference.designPattern;
public interface Command
{
   public void execute();
}

Concrete command class: AdditionCommand.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.j2eereference.designPattern;
public class AdditionCommand implements Command
{
  AdditionReceiver additionReceiver;
  public AdditionCommand(AdditionReceiver additionReceiver)
  {
    this.additionReceiver = additionReceiver;
  }
  @Override
  public void execute()
{
   // TODO Auto-generated method stub
   System.out.println("Request for addition operation has been raised");
   additionReceiver.additionAction();
}
}

 

Receiver Class: AdditionReceiver

Java
1
2
3
4
5
6
7
8
9
package com.j2eereference.designPattern
 
public class AdditionReceiver
{
   public void additionAction()
   {
     System.out.println("Performing Addition operation");
   }
}

OutPut:

Request for addition operation has been raised
Performing Addition operation

RealTime Use Cases :

  • Implementation of Runnable , callable follows command design pattern
  • In Real life, when you place order in restaurant then you are client who gives command to waiter, waiter then contact the chef to prepare your order.
  • Remote control is an other good example of command design pattern as when you press On button then TV starts up and when you press Off button the it accept your request and closes the TV.

Related Posts

  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • GOF Design Patterns
  • History and Need for Design Patterns
  • Template Design Pattern
  • Proxy design pattern
  • Singleton in Clustered Environment
  • Observer pattern

Filed Under: Design Patterns

Reader Interactions

Trackbacks

  1. Java Revision – QuickInterviewPrep says:
    July 2, 2017 at 3:57 am

    […]  Command Design Pattern : http://209.97.166.197/command-design-pattern/ […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.