No history yet

Introduction to Command Pattern

What is the Command Pattern?

Imagine you're at a restaurant. You decide what you want to eat and tell the waiter. The waiter writes your order on a ticket and gives it to the kitchen. The chef reads the ticket and prepares your meal. You don't talk to the chef directly, and the waiter doesn't need to know how to cook. The order ticket is the key. It contains everything needed to get the job done.

The Command Pattern works in a similar way. It's a behavioral design pattern that turns a request into a stand-alone object. This object holds all the information about the request, such as the action to be performed and any necessary parameters.

The main idea is to separate the object that issues a request from the object that actually performs the action.

The Key Players

The pattern has four main participants. Let's stick with our restaurant analogy to understand them.

RoleDescriptionRestaurant Example
ClientCreates the command object and configures it with a receiver.You, the customer, deciding to order a burger.
InvokerHolds a command object and asks it to execute the request.The waiter who takes your order ticket.
CommandThe object that encapsulates the request. It binds together a set of actions on a specific receiver.The order ticket for a burger. It has an execute() method.
ReceiverThe object that knows how to perform the work to carry out the request.The chef who knows the recipe and cooks the burger.

The Client creates a Command object that tells the Receiver what to do. It then passes this command to the Invoker, which triggers the command at the right time. The Invoker doesn't know anything about the Receiver; it only knows about the Command's interface.

Why Bother?

This setup might seem a bit complicated, but it offers some powerful benefits.

Decoupling: The object making the request (Invoker) is completely separated from the object performing the action (Receiver). The Invoker only needs to know how to execute a command. You can add new commands without changing the Invoker's code at all.

Queues and Logs: Since commands are objects, they can be stored. You can put commands in a queue to be executed later, one by one. This is great for scheduling tasks or handling operations in a specific order. You can also save a history of commands, creating a log of all actions taken in an application.

Undo/Redo Functionality: This is one of the most common uses of the Command Pattern. To support undo, the Command object can have an undo() method that reverses the action of its execute() method. When a user performs an action, you execute the command and push it onto a history stack. To undo, you pop the last command from the stack and call its undo() method.

You see this pattern in action all the time. Think of a text editor's undo and redo feature, a remote control for your TV, or recording a macro that replays a series of steps in a program. In each case, an action is packaged as an object and handled from there.

Behavioral design patterns are a subset of design patterns in software engineering that deal with the interaction and responsibilities of objects.

Now, let's test your understanding of these concepts.

Quiz Questions 1/5

What is the primary purpose of the Command Design Pattern?

Quiz Questions 2/5

In the restaurant analogy, the waiter takes your order and gives it to the kitchen. Which component of the Command Pattern does the waiter represent?

The Command Pattern provides a clean way to separate the "what" from the "how" in your code, leading to more flexible and maintainable systems.