728x90 AdSpace

Saturday, 29 July 2017

Design Patterns - Command Pattern

Command Pattern

Definition

The Command pattern is used to create objects that represents actions and events in an application. A command object encapsulates an action or event and contains all information required to understand exactly what has happened. By passing the command object as a parameter we can, anywhere needed extract information about occurred actions and events.

Where to use

• Where you want a action that can be represented in many ways, like dropdown menu, buttons and popup menu.
• To create undo/redo functionality.

Benefits

• A command object is a possible storage for procedure parameters. It can be used while assembling the parameters for a function call and allows the command to be set aside for later use.
• A class is a suitable place to collect code and data related to a specific action or event.
•It allows the reaction to a command to be executed some time after it has occurred.
•Command objects enables data structures containing multiple commands.
•Command objects supports undo-able operations, provided that the command objects are stored (for example in a linked list).

Drawbacks/consequences

The main disadvantage of the Command pattern seems to be a proliferation of little classes that clutter up the program. However, even in the case where we have separate click events, we usually call little private methods to carry out the actual function. It turns out that these private methods are
just about as long as our little classes, so there is frequently little difference in complexity between building the Command classes and just writing more methods. The main difference is that the Command pattern produces little classes that are much more readable.

Command Pattern Class Diagram


Command Pattern example


package com.designpattern.command;
/** Command interface */
public interface Command{
void execute();
}
package com.designpattern.command;
/** The Command for turning on the light */
public class TurnOnLightCommand implements Command{
private Light theLight;
public TurnOnLightCommand(Light light){
this.theLight=light;
}
public void execute(){
theLight.turnOn();
}
}
package com.designpattern.command;
/** The Command for turning off the light */
public class TurnOffLightCommand implements Command{
private Light theLight;
public TurnOffLightCommand(Light light){
this.theLight=light;
}
public void execute(){
theLight.turnOff();
}
}

package com.designpattern.command;
/** Receiver class */
public class Light{
public Light(){ }
public void turnOn(){
System.out.println("The light is on");
}
public void turnOff(){
System.out.println("The light is off");
}
}
package com.designpattern.command;
/** Invoker class*/
public class Switch {
private Command flipUpCommand;
private Command flipDownCommand;
public Switch(Command flipUpCmd,Command flipDownCmd){
this.flipUpCommand=flipUpCmd;
this.flipDownCommand=flipDownCmd;
}
public void flipUp(){
flipUpCommand.execute();
}
public void flipDown(){
flipDownCommand.execute();
}
}
package com.designpattern.command;
/** Test class */
public class TestCommand{
public static void main(String[] args){
Light l = new Light();
Command switchUp = new TurnOnLightCommand(l);
Command switchDown = new TurnOffLightCommand(l);
Switch s = new Switch(switchUp,switchDown);
s.flipUp();
s.flipDown();
}
}
When executing TestCommand the result is:
c:>The light is on
c:>The light is off

The following class-diagram illustrates the structure for the Command Pattern example.


Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Command Pattern Rating: 5 Reviewed By: Annamalai Thangaraj