Application Development, Product to Market
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

Command Design Pattern Example

4/14/2016

0 Comments

 
Command pattern is a behavioral pattern in GoF as it allows us to decouple between the Invoker (a.k.a. sender, an object that invokes an operation) and the Receiver (an object that receives the request to execute a certain operation). 

 Also known as an action or transaction pattern.
Picture
Picture
source: GoF

overview

Command, an interface that encapsulate the request itself, promotes "invocation of a method on an object" via an Execute() method. Each concrete Command class provides different Execute() implementations to invoke the request, and contains a Receiver as an instance variable by which the request will be carried out. An optional Unexecute() method can be added to the Command for undo operations to reverse the effects of a preceding Execute operation using whatever undo information Execute stored. 
​
Command is implemented as an Adaptor between Invoker and Receiver.

Invoker, an aggregation of Commands, asks the command to carry out the request. In the above figure, MenuItem is the Invoker. The Switch class as seen in our code example below is also an Invoker. An Invoker invokes command.Execute() operation.

Receiver, encapsulated by Command, knows how to carry out a request. Any class may serve as a Receiver. In the above figure, Document and Application are the Receivers.

The decoupling between the Invoker/Sender and the Receiver is achieved by the Command served as an Adaptor between the Invoker and the Receiver. Specifically, Invoker contains Commands, each Command in terms contains a Receiver.

command code example

Command class:
public interface Command {
    public abstract void execute ( );
}

​class LightOnCommand implements Command{
    private Light light;
    public LightOnCommand(Light L){
        light = L;
    }
    public void execute(){
        light.turnOn();
    }
}

class LightOffCommand implements Command{
    private Light light;
    public LightOffCommand(Light L){
        light = L;
    }
    public void execute() {
        light.turnOff();
    }
}
Invoker class:
class Switch {
    private Command UpCommand, DownCommand;
 
    // register Commands with the invoker

    public Switch(Command Up, Command Down) {
        UpCommand = Up;  
        DownCommand = Down;
    }

    void flipUp(){    // invoker calls back concrete Command,
                      // which executes the Command on the receiver 
        UpCommand.execute();                           
    }
 
    void flipDown(){
        DownCommand.execute();
    }
}
Receiver class:
class Light {
    public void turnOn( ) {
        System.out.println("Light is on ");
    }
    public void turnOff( ) {
        System.out.println("Light is off");
    }
}

when to use

  1. Decouple request senders and receivers.
  2. Use the Command pattern to implement callbacks. 
  3. Support undo and redo.
  4. Use Command to represent requests as objects by Chain of Responsibility.

comparison

memento vs. command
  • Both can act as magic tokens to be passed around and invoked at a later time.
  • In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time.
  • Both can support muti-level undos.
  • Memento pattern's capturing and restoring entire state is more expensive then Command patterns for implementing Undo/Redo.

references

  • Learn how to implement the Command pattern in Java
  • Multilevel Undo and Redo Implementation in C# - Part II (Using Command Pattern)
  • Command Design Pattern
0 Comments



Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar