commandPattern

Type of the pattern:

The Command Pattern is a behavioral design patterns (design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.)

 

Problem it solves:

Command pattern decouples the object that invokes the action from the object that performs the action.

Command pattern has following components:

  1. Client: Client is responsible for creating concrete command and setting its receiver.
  2. Invoker: asks the command to carry out the request.
  3. Command: declares an interface for executing an operation.
  4. ConcreteCommand: The ConcreteCommand defines a binding between an action and a receiver. The invoker makes request by calling execute () and the ConcreteCommand carries it out by calling one or more actions on the receiver.
  5. Receiver: It knows how to perform the action

 

Examples:

Let’s understand each of these components by an example:

Order processing in a hotel can be used as an example to explain above components:

  1. Client: Client is responsible for creating order (concrete command) and setting its receiver(cook)
  2. Waiter: Waiter knows how to place order at food counter. It is the Invoker. Client is unaware about how order reaches or processed at the food counter.
  3. Order: Order is a command with cook set inside it which will actually process the order. Waiter is completely unaware about what all instructions given to cook to prepare meal.
  4. Receiver: Cook is the actual executor of the action.

Below is the class diagram for Command design pattern:

commandPattern

Let’s understand importance of the command pattern using another example:

Suppose a system has Organisation as an entity. Every time organization is created a set of validations is to be run which is communicated to development team. It’s known that validations are updated/added frequently as business requirement changes. Below class diagram shows how command pattern can be applied to solve this requirement

commandPattern1

 

 

 

 

CommandInterface:

public interface IValidationCommand {

void execute();

}

ConcreteCommand:

 public class OrgNameAlphanumericCheck implements IValidationCommand{

private Organisation org;

public OrgNameAlphanumericCheck(Organisation org) {

super();

this.org = org;

}

@Override

public void execute() {

org.checkIfNameIsAlphanumeric();

}

}

Receiver:

public class Organisation {

private static final int MAXIMUM_LENGTH = 0;

private static final int MINIMUM_LENGTH = 0;

private String name;

// Many other organisation parameters

public Organisation(String name/* other constructor params */) {

super();

this.name = name;

 

/* other intialization statements */

}

 

public void checkIfNameIsAlphanumeric() {

String pattern = “^[a-zA-Z0-9]*$”;

if (name.matches(pattern)) {

throw new RuntimeException(“Special charecters are not allowed”);

}

}

 

public void checkNameMaxLength() {

if(name.length() > MAXIMUM_LENGTH){

throw new RuntimeException(“Organisation length exceeds maximum lenght”);

}

}

 

public void checkNameMinLength() {

if(name.length() < MINIMUM_LENGTH){

throw new RuntimeException(“Organisation length is shorter than required length: “+MINIMUM_LENGTH);

}

}

}

Invoker:

public class CreateOrgValidationChain {

List<IValidationCommand> validationCommands = new ArrayList<IValidationCommand>();

public void setValidationCommand(IValidationCommand validationCommand) {

validationCommands.add(validationCommand);

}

public void validate() {

for (IValidationCommand validationCommand : validationCommands) {

validationCommand.execute();

}

}

}

Client:

public class OrganisationService {

public void createOrganisation(){

Organisation organisation = new Organisation(“Swabhav Techalabs”);

CreateOrgValidationChain organisationValidationChain = new      CreateOrgValidationChain();

OrgNameAlphanumericCheck alphanumericCheckCommand = new              OrgNameAlphanumericCheck(organisation);

OrgNameMaximumLengthCommand orgNameMaximumLengthCommand = new OrgNameMaximumLengthCommand(organisation);

OrgNameMinimumLengthCommand orgNameMinimumLengthCommand = new OrgNameMinimumLengthCommand(organisation);

 

organisationValidationChain.setValidationCommand(alphanumericCheckCommand);

organisationValidationChain.setValidationCommand(orgNameMaximumLengthCommand);

organisationValidationChain.setValidationCommand(orgNameMinimumLengthCommand);

 

organisationValidationChain.validate();

}

}

Command Pattern

Post navigation


Leave a Reply

Your email address will not be published. Required fields are marked *