Design Patterns Command Pattern - Design Patterns

What is Design Pattern Command Pattern?

Command pattern is a data determined propose pattern and falls under behavioural pattern group. A request is wrapped under an object as command and passed to invoker object. Invoker object looks for the correct object which can handle this command and passes the command to the matching object which executes the command.

Implementation

We have formed an interface Order which is acting as a command. We have formed a Stock class which acts as a request. We have real command classes Buy Stock and Sell Stock implementing Order line which will do definite command processing. A class Broker is created which acts as an invoker entity. It can take and place preparation.

Broker entity uses command pattern to recognize which object will implement which command based on the type of command. CommandPatternDemo, our demo class, will use Broker class to express charge pattern.
command_pattern_uml_diagram

Step 1

Create a command interface.

Order.java

Step 2

Create a request class.

Stock.java

Step 3

Create concrete classes implementing the command interface.

BuyStock.java

SellStock.java

Step 4

Create command invoker class.

Broker.java

Step 5

Use the Broker class to take and perform information.

CommandPatternDemo.java

Step 6

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics