- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Command Pattern
Command pattern is a data driven design pattern and falls under behavioral pattern category. A request is wrapped under an object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and passes the command to the corresponding object which executes the command.
Implementation
We have created an interface Order which is acting as a command. We have created a Stock class which acts as a request. We have concrete command classes BuyStock and SellStock implementing Order interface which will do actual command processing. A class Broker is created which acts as an invoker object. It can take and place orders.
Broker object uses command pattern to identify which object will execute which command based on the type of command. CommandPatternDemo, our demo class, will use Broker class to demonstrate command pattern.
Step 1
Create a command interface.
Order.java
pubpc interface Order { void execute(); }
Step 2
Create a request class.
Stock.java
pubpc class Stock { private String name = "ABC"; private int quantity = 10; pubpc void buy(){ System.out.println("Stock [ Name: "+name+", Quantity: " + quantity +" ] bought"); } pubpc void sell(){ System.out.println("Stock [ Name: "+name+", Quantity: " + quantity +" ] sold"); } }
Step 3
Create concrete classes implementing the Order interface.
BuyStock.java
pubpc class BuyStock implements Order { private Stock abcStock; pubpc BuyStock(Stock abcStock){ this.abcStock = abcStock; } pubpc void execute() { abcStock.buy(); } }
SellStock.java
pubpc class SellStock implements Order { private Stock abcStock; pubpc SellStock(Stock abcStock){ this.abcStock = abcStock; } pubpc void execute() { abcStock.sell(); } }
Step 4
Create command invoker class.
Broker.java
import java.util.ArrayList; import java.util.List; pubpc class Broker { private List<Order> orderList = new ArrayList<Order>(); pubpc void takeOrder(Order order){ orderList.add(order); } pubpc void placeOrders(){ for (Order order : orderList) { order.execute(); } orderList.clear(); } }
Step 5
Use the Broker class to take and execute commands.
CommandPatternDemo.java
pubpc class CommandPatternDemo { pubpc static void main(String[] args) { Stock abcStock = new Stock(); BuyStock buyStockOrder = new BuyStock(abcStock); SellStock sellStockOrder = new SellStock(abcStock); Broker broker = new Broker(); broker.takeOrder(buyStockOrder); broker.takeOrder(sellStockOrder); broker.placeOrders(); } }
Step 6
Verify the output.
Stock [ Name: ABC, Quantity: 10 ] bought Stock [ Name: ABC, Quantity: 10 ] soldAdvertisements