Wednesday, March 22, 2017

Command Design Pattern in Java

INTRODUCTION

Command design pattern is categorized under Behavioral Design Pattern. Encapsulate the request and pass it to appropriate processor to process the request. It provides callback mechanism, so that we can get the output for the request.

KEY ENTITIES

  1. Command - is the one who will have the definition of the action.
  2. Invoker - is the one who will receive the request but don't know how to process the request. It uses the command to initiate the process.
  3. Receiver - is the one who will know how to process the request and return back the response.
  4. Client - is the one who will creates the request, it may be one or multiple.

USECASE

Consider the use case of food ordering in a restaurant.
  1. Customer who will order the food to a Waiter.
  2. Waiter will get the order details, but don't know how to prepare the ordered food.
  3. Waiter will pass the order details to the Chef who will know how to prepare different food items.
  4. Chef will prepare the ordered foods and handover to the Waiter and he will serve (call back) to the customer.
Here
Command = FoodOrder
Invoker = Waiter
Receiver = Chef
Client = Customer
Now we will try to implement the restaurant usecase.
Let us create a Food Object.
public class Food {
 private String name;
 private int quantity;
 private int price;
  
 // getters and setters
}

Create a Command Interface with desired action defined.
public interface FoodOrder {
 Food prepareFood();
}

Create implementation for the Command.
public class IndianFoodOrder implements FoodOrder {
 public Food prepareFood() {
  // logic to prepare the indian food.
 }
}
public class ChineseFoodOrder implements FoodOrder {
 public Food prepareFood() {
  // logic to prepare the chinese food.
 }
}


Create the Invoker.
public class Waiter {
 // store all the customer food .
 private List<FoodOrder> foodOrders = new ArrayList<FoodOrder>();
  
 // store all the ordered foods for the call back to customer.
 private List<Food> orderedFoods = new ArrayList<Food>();
 // store the orders.
 public void placeFoodOrder(FoodOrder food) {
   foodOrders.add(food);
 }
 // get the prepared food.
 public List<Food> serve() {
   // logic to prepare the food.
   return orderedFoods;
 }
}

Create the Receiver.
public class Chef {
public Food preparChineseFood(String food) {
   // logic to prepare the Chinese food.
 }
 public Food prepareIndianFood(String food) {
  // logic to prepare the Indian food.
 }
}

Create the Client.
public class Customer {
 public static void main(String[] args) {
   // Call a Waiter
   Waiter waiter = new Waiter();
 
   // Order Indian Food and pass the order to a Chef
   waiter.placeFoodOrder(new IndianFoodOrder("idly", new Chef()));
 
   // Order Chinese Food and pass the order to a Chef
   waiter.placeFoodOrder(new ChineseFoodOrder("noodles", new Chef()));
   // Get the ordered foods and enjoy.
   for (Food food : waiter.serve()) {
     System.out.println("Food Name: " + food.getName()
                      + "\nQuantity: " + food.getQuantity()
                      + " plates\nPrice: "
                      + food.getPrice() + " rupees\n");
   }
 }
}

SUMMARY

  1. Command design pattern reduces the decoupling between Invoker and Receiver.
  2. Helps to achieve the callback mechanism.
  3. Command pattern will be implemented like switch off and on mechanism (undo and redo operations).

Hope this example clarifies the Command Design Pattern.
Please share your thoughts in comment box.
Happy Knowledge Sharing!!!

No comments :

Post a Comment