Core Java Design Pattern : Behavioral Pattern : Command Design Pattern
Command Design Pattern |
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Also Known As : Action, Transaction
Sometimes it’s necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request. For example, user interface toolkits include objects like buttons and menus that carry out a request in response to user input. But the toolkit can’t implement the request explicitly in the button or menu, because only applications that use the toolkit know what should be done on which object. As toolkit designers we have no way of knowing the receiver of the request or the operations that will carry it out.
Use the Command pattern when you want to
- parameterize objects by an action to perform, as MenuItem objects did above. You can express such parameterization in a procedural language with a callback function, that is, a function that’s registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks.
- specify, queue, and execute requests at different times.
- support undo. The Command’s Execute operation can store state for reversing its effects in the command itself.
- support logging changes so that they can be reapplied in case of a system crash.
- structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions..
Command
- declares an interface for executing an operation.
ConcreteCommand (PasteCommand, OpenCommand)
- defines a binding between a Receiver object and an action.
- implements Execute by invoking the corresponding operation(s) on Receiver.
Client (Application)
- creates a ConcreteCommand object and sets its receiver.
Invoker (MenuItem)
- asks the command to carry out the request.
Receiver (Document, Application)
- knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver.
Examples |
Customer.java
package com.javaskool.command;
public class Customer {
private String name;
public Customer(String name)
{
this.name=name;
}
}
Order.java
package com.javaskool.command;
public class Order {
String command;
public Order(String command)
{
this.command=command;
}
public void setCommand(String command)
{
this.command=command;
}
public String getCommand()
{
return command;
}
}
Food.java
package com.javaskool.command;
public class Food {
Order order;
public Food()
{
}
public Food(Order order)
{
this.order=order;
}
public String toString() {
return order.getCommand() +" has completed";
}
public void setOrder(Order order)
{
this.order=order;
}
public Order getOrder()
{
return order;
}
}
Cook.java
package com.javaskool.command;
public class Cook {
public Food prepareOrder(Order order,Waiter waiter)
{
Food food=getCookedFood(order);
return food;
}
public Food getCookedFood(Order order) {
Food food = new Food(order);
return food;
}
}
Waiter.java
package com.javaskool.command;
public class Waiter {
public Food takeOrder(Customer cust,Order order)
{
Cook cook=new Cook();
Food food=cook.prepareOrder(order,this);
return food;
}
}
TestDrive.java
package com.javaskool;
import com.javaskool.command.Customer;
import com.javaskool.command.Food;
import com.javaskool.command.Order;
import com.javaskool.command.Waiter;
public class TestDrive {
public static void main(String[] args) {
Waiter waiter=new Waiter();
Customer cust=new Customer("Samant");
Order order= new Order("Cold drink");
Food f=waiter.takeOrder(cust,order);
System.out.println(f);
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
Cold drink has completed
Recent Comments