Command Pattern

Command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time.

The intent of this pattern, according to Design Patterns by Gamma et al, is to

  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

This pattern is used for issuing requests to objects without knowing anything about the operation being requested or the receiver of the request.

 

Structure

Command-pattern-structure

Figure: Command Pattern Structure

Participants

Command

  • declares an interface for executing an operation

ConcreteCommand

  • extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.

Client

  • creates a ConcreteCommand object and sets its receiver;

Invoker

  • asks the command to carry out the request;

Receiver

  • knows how to perform the operations

Here is how command pattern works – a command object knows about receiver and invokes a method of the receiver. Values for parameters of the receiver method are stored in the command. The receiver then does the work. An invoker object knows how to execute a command, and optionally does logging about the command execution. The invoker does not know anything about a concrete command, it knows only about command interface. Both an invoker object and several command objects are held by a client object. The client decides which commands to execute at which points. To execute a command, it passes the command object to the invoker object.

The following sequence diagram explains more about he working of command pattern.

command-pattern-sequence

Figure: Command Pattern sequence diagram

Thus, command pattern decouples the invoker from receiver and request it carries out.

 

Example

Let us consider a case in which we have to write the software that goes into a mobile phone screen lock.

The phone screen is locked and unlocked on the basis of the button pressed. 

The following class diagram explains more about the situation.

command pattern example

Figure: Command Pattern Example

 

Java Implementation

Let us first create an interface for command.

/**
 * The Command interface
 */
public interface Command {
    void execute();
}

The receiver class is:

/**
 * receiver class
 */
public class MobilePhone {
    public void unlockPhone(){
        System.out.println("Phone screen unlocked.");
    }
    public void lockPhone(){
        System.out.println("Phone screen locked.");
    }
}

Now, the concrete commands are:

/**
 * ConcreteCommand - The Command for locking the phone
 */
public class LockPhoneCommand implements Command {
    private MobilePhone control;

    public LockPhoneCommand(MobilePhone control) {
        this.control = control;
    }

    @Override
    public void execute() {
        control.lockPhone();
    }
}
/**
 * ConcreteCommand - The Command for unlocking the phone
 */
public class UnlockPhoneCommand implements Command {
    private MobilePhone control;

    public UnlockPhoneCommand(MobilePhone control) {
        this.control = control;
    }

    @Override
    public void execute() {
        control.unlockPhone();
    }
}

The invoker class is:

/**
 * Invoker class
 */
public class PowerButton {
    private List<Command> commands = new ArrayList<Command>();

    public void pressButton(Command command){
        commands.add(command);  //for undoing capability; optional in this case
        command.execute();
    }
}

Finally, the client code is:

/**
 * client
 */
public class Application {
    public static void main(String[] args) {
        //creating commands
        MobilePhone control= new MobilePhone();
        Command lockPhone = new LockPhoneCommand(control);
        Command unlockPhone = new UnlockPhoneCommand(control);
        //calling invoker
        PowerButton button= new PowerButton();
        //unlocking phone screen
        button.pressButton(unlockPhone);
        //locking phone screen
        button.pressButton(lockPhone);
    }
}

The output of the program is:

Phone screen unlocked.
Phone screen locked.
Composite Pattern
Iterator Pattern