Command Design Pattern Example

Command Design Pattern:  The Command pattern is known as a behavioral pattern and is used to represent and encapsulate all the information required to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters. Command pattern allows the requester of a particular action to be decoupled from the object that performs the action.


The Command Pattern is useful when we need to do the following task:
  • A history of actions is needed
  • You need callback functionality, like listeners in java
  • Calls or Requests need to be handled at different times or in different orders
  • The invoker should be decoupled from the object handling the invocation.

Lets take an example, Suppose we have an electric engine and we want to start and stop this engine on press or click of a button. We will use command patter to do this.

Steps to use: Command is an interface with execute method, and can be implemented by following the below steps.

  • Create an instance of a command implementation and associates it with a receiver.
  • Create an invoker which will invoke the command to perform an action.
  • Create a Command implementation which creates a binding between the receiver and an action.
  • Receiver is the object that knows the actual steps to perform the action or will perform the actual action.


Now we will write these classes one by one.
Engine.java
public class Engine {

public void start() {
System.out.println("Engine Started");
}

public void stop() {
System.out.println("Engine Stopped");
}
}


Write an interface Command.


public interface Command{
void execute();
}


Write the implementation of this command interface as StartCommand.java and StopCommand.java


public class StartCommand implements Command{
           private Engine engine;
public StartCommand(Engine engine){
this.engine = engine;
}
@Override
public void execute() {
//do the action here
engine.start();
}
 }



public class StopCommand implements Command{
             private Engine engine;
public StopCommand(Engine engine){
this.engine = engine;
}
@Override
public void execute() {
//do the action here
engine.stop();
}
}

Finally write a CommandDemo.java which will show how to use invoke these commands.


public class CommandDemo {

    public static void main(String[] args) {
    Engine engine = new Engine();// receiver
    Command startCommand = new StartCommand(engine);// concrete command
             Command stopCommand = new StopCommand(engine);// concrete command
   
    Button button = new Button(null);// invoker
    button.setCommand(startCommand);
    button.click();//invoke command
    button.setCommand(stopCommand);
    button.click();//invoke command
}
}


Demo output:

Engine Started
Engine Stopped

As you can see, the invoker is decoupled from the action performed by the receiver, and has no direct knowledge of the action being performed by the receiver. The invoker invokes a command, and the command executes the appropriate action of the receiver. Thus, the invoker can invoke commands without knowing the details of the action to be performed. In addition, this decoupling means that changes to the receiver's action don't directly affect the invocation of the action.

No comments :

Post a Comment