Command design pattern

The command design pattern is a behavioral design pattern in which object is used to represent and encapsulates the all information need to call a method at later time.

Using command design pattern the interaction between client and service is more decoupled manner.

In command design pattern we have below components

1)Command:

It is a parent interface for all the commands, it defines the contract.

2)Command Implementation:

These classes are the implementation of command interface, these classes are responsible for calling actual service code.

2)Invoker:

It will act as a bridge between the client request and command object.

3)Receiver

Is nothing but service implementation class, which contain core logic.

4)Client:

Client will instantiate appropriate command  and it will execute the action command.

Example

//Service interface

package com.javamirchi.command.service;

public interface Vehicle {

public void startVehicle();

public void stopVehicle();

}

//Service implementation (Receiver)

package com.javamirchi.command.service;

public class Bus implements Vehicle{

public void startVehicle() {

System.out.println(“BUS has started…”);

}

public void stopVehicle() {

System.out.println(“BUS has stopped…”);

}

}

//Service implementation2 (Receiver)

package com.javamirchi.command.service;

public class Car implements Vehicle{

public void startVehicle() {

System.out.println(“CAR has started…”);

}

public void stopVehicle() {

System.out.println(“CAR has stopped…”);

}

}

//Command Interface

package com.javamirchi.command;

public interface Command {

public void execute();

}

//Command Implementation.

package com.javamirchi.command;

import com.javamirchi.command.service.Bus;

public class StartBus implements Command {

Bus bus;

public StartBus(Bus bus) {

this.bus = bus;

}

public void execute() {

bus.startVehicle();

}

}

//Command Implementation.

package com.javamirchi.command;

import com.javamirchi.command.service.Car;

public class StartCar implements Command {

Car car;

public StartCar(Car car) {

this.car = car;

}

public void execute() {

car.startVehicle();

}

}

//Invoker

package com.javamirchi.command;

public class Invoker {

Command command;

public Invoker(Command command) {

this.command = command;

}

public void processRequest() {

command.execute();

}

}

//Client

package com.javamirchi.client;

import com.javamirchi.command.Command;

import com.javamirchi.command.Invoker;

import com.javamirchi.command.StartBus;

import com.javamirchi.command.StartCar;

import com.javamirchi.command.service.Bus;

import com.javamirchi.command.service.Car;

public class CilentTest {

public static void main(String[] args) {

Car car = new Car();

Command command = new StartCar(car);

Invoker invoker = new Invoker(command);

invoker.processRequest();

Bus bus = new Bus();

command = new StartBus(bus);

invoker = new Invoker(command);

invoker.processRequest();

}

}

Output:

CAR has started…

BUS has started…

command2

Benefits:

i)We can store list of command for later user.

ii)A class is a good place to store procedure

iii)Best way to implement callback functionality

iv)Decoupling the service implementation class and receiver.

Drawback:

The only drawback is  number of classes will be increased.