command pattern

Command Pattern

Command Pattern Definition

 
Command pattern encapsulates a request as an object, thereby letting you parameterize clients with different
requests, queue or log requests, and support undoable operations. [GOF definition]
 

Understanding Command Pattern

 
command pattern

 

Following terms are associated with the Command Pattern :
 

Client

The client object is responsible for creating a ConcreteCommand and setting its Receiver.
 

Invoker

The Invoker holds a command and at some point asks the command to carry out a request by invoking its execute() method.
 

Command

Command declares an interface for all commands. A command is invoked through its execute() method, which aks the Receiver to perform an action.
 

Receiver

The Receiver knows how to perform the work needed to carry out the request.

 

Command Pattern Demo

 

To implement Command Pattern, lets first create a ICommand interface that declares a method doSomething.

package com.topjavatutorial.patterns.command;

public interface ICommand {
  public void doSomething();
}


 
Now, lets create implementations of ICommand interface.
package com.topjavatutorial.patterns.command;

public class CommandOne implements ICommand {

  Receiver receiver;
  
  public CommandOne(Receiver receiver){
    this.receiver = receiver;
  }
  
  @Override
  public void doSomething() {
    receiver.operation1();
  }

}


 

package com.topjavatutorial.patterns.command;

public class CommandTwo implements ICommand {

  Receiver receiver;
  
  public CommandTwo(Receiver receiver){
    this.receiver = receiver;
  }
  
  @Override
  public void doSomething() {
    receiver.operation2();
  }

}


 

Also, lets create a Receiver class with methods operation1() and operation2().

package com.topjavatutorial.patterns.command;

public class Receiver {
  public void operation1(){
    System.out.println("Performing operation1");
  }
  
  public void operation2(){
    System.out.println("Performing operation2");
  }
}


 

The Invoker is unaware of the Command implementations. Lets add an executeCommand() method in it that will be invoked for the ICommand implementations.

package com.topjavatutorial.patterns.command;

public class Invoker {
  ICommand cmd;
  
  public void executeCommand(ICommand cmd){
    this.cmd = cmd;
    cmd.doSomething();
  }
}


 

Now, lets create the client CommandPatternDemo class to execute the commands.

package com.topjavatutorial.patterns.command;

public class CommandPatternDemo {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Receiver receiver = new Receiver();
    
    ICommand one = new CommandOne(receiver);
    ICommand two = new CommandTwo(receiver);
    
    Invoker invoker = new Invoker();
    invoker.executeCommand(one);
    invoker.executeCommand(two);
  }

}


 

Output

 
Performing operation1
Performing operation2

 

Advantages of the Command pattern

 

The command pattern decouples an object making the request from the object that is doing it.

This pattern helps in terms of extensibility as we can add a new command without changing the existing code.

Commands can be used to implement logging and transactional systems.
 

Disadvantages of the Command pattern

 

The main disadvantage of the Command pattern is the increase in the number of classes for each individual command.

 

You may also like :

 
Singleton Design Pattern
 
Enterprise Design Patterns in Java
 
Observer Pattern in Java
 
Proxy Pattern in Java

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags