11.5.3 - Command Pattern
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Introduction to Command Pattern
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Today, we are discussing the Command Pattern, a behavioral design pattern that encapsulates a request as an object. It allows for parameterization with different requests. Can anyone share what they think parameterization means?
I think it's when you can customize or change something to fit different needs.
Exactly! So, the Command Pattern allows users to customize actions by defining specific commands. It helps separate the responsibilities of different parts of our program.
What type of things can we encapsulate as a command?
Great question! Any action that can be triggered like turning on a light or processing an order can be encapsulated as commands.
Structure of Command Pattern
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
The Command Pattern typically involves three main roles: a Command interface, Concrete Command classes, and the Receiver. Can someone tell me what the Receiver does?
Isn’t that the object that actually does the action?
Exactly! The Receiver holds the business logic. Concrete Commands implement the Command interface and invoke methods on the Receiver. Can anyone provide an example based on our everyday life?
Ordering food could be an example! The command would be to place an order, and the restaurant would be the receiver.
Perfect analogy! In software, it works the same way.
Real-Life Examples of Command Pattern
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Let’s look at some real-world applications. The Command Pattern can be found in GUI libraries, where actions like button clicks trigger commands. Can anyone think of an application you use that might utilize this pattern?
I use an editor application with undo/redo functionality, like Word. Does it use the Command Pattern?
Yes! The undo action can be encapsulated as a command that reverses the last action taken. It’s a powerful feature!
What are some advantages of using the Command Pattern?
The primary advantages are flexibility, reusability, and support for undo operations. It simplifies managing complex operations.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
In the Command Pattern, requests are encapsulated into command objects, enabling operations to be parameterized and making it easier to create flexible and reusable code. This pattern supports undoable operations and helps with decoupling sender and receiver.
Detailed
Command Pattern in Design Patterns
The Command Pattern is a behavioral design pattern that encapsulates a request as an object. This pattern allows clients to parameterize with different requests, queue or log requests, and support undoable operations. In this approach, a command interface defines a method for executing a command. Concrete command classes implement this interface to define specific actions. Additionally, this pattern promotes decoupling between the sender (caller) and receiver (the object that carries out the command). By using the Command Pattern, developers can enhance the flexibility and reusability of their code.
Key Points:
- Encapsulation of Actions: Requests are encapsulated within command objects.
- Parameterization: Clients can specify the commands they need to execute, leading to greater flexibility.
- Decoupling Sender and Receiver: The sender (invoker) knows only the command interface, not the concrete implementation. This promotes loose coupling.
- Supports Undo Operations: The pattern can facilitate undo operations if commands are designed to keep track of states.
Significance:
The Command Pattern is widely used in applications where operations can be executed in a manner that is independent of the user interface and actions they invoke.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Overview of the Command Pattern
Chapter 1 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Encapsulates a request as an object, thereby letting users parameterize clients with different requests.
Detailed Explanation
The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object. This allows the parameters for executing a request to be stored and enables users to queue requests or log them. Instead of directly calling a method on an object, you create a command object that contains all the information needed to perform the action, which can then be executed at a later time.
Examples & Analogies
Consider a remote control for a television. Each button on the remote (like 'power', 'volume up', or 'channel change') encapsulates a command. When you press a button, it sends a request to the TV to perform a certain action. You can think of the remote control as the invoker that holds various command objects (the buttons) that can be pressed anytime to perform different actions.
Command Interface
Chapter 2 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
interface Command {
void execute();
}
Detailed Explanation
In the Command Pattern, a key component is the Command interface. This interface declares a method called 'execute'. This method will be implemented by concrete command classes. The intent is to define a common interface for all command objects so that they can be treated uniformly regardless of their specific actions.
Examples & Analogies
Think of the Command interface as a job description for various professions. Just as different jobs (like 'teacher', 'doctor', 'engineer') implement the tasks defined in their job descriptions (teaching, healing, designing), different commands will implement the execute method, which embodies their specific actions.
Concrete Command: LightOnCommand
Chapter 3 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOn();
}
}
Detailed Explanation
The Concrete Command is a specific implementation of the Command interface. In this example, the LightOnCommand class implements the Command interface and holds a reference to a Light object. The execute method in this class triggers the 'turnOn' method of the Light object, encapsulating the action of turning on a light as a distinct command.
Examples & Analogies
Imagine a specific recipe for making a sandwich. The LightOnCommand is like a recipe that says 'To make a sandwich, take bread, add fillings, and put it together'. In this case, if you want to execute the command to turn on the light, you follow the recipe (execute method) to get the result (the light is turned on).
Key Concepts
-
Encapsulation: The process of wrapping the data and the methods that operate on the data within one unit, which is a Command object.
-
Parameterization: The technique of specifying parameters to allow for different requests.
-
Decoupling: Separating the sender and receiver to promote lower dependency among software components.
Examples & Applications
Creating a light command: A Light class with a method to turn on/off lights can be encapsulated into a Command class to control the light's state.
Text editor undo feature: Actions like typing, cutting, or pasting can be organized as commands to allow users to undo/redo their operations.
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
Commands are for action, not for slack, they send the call, and take you back.
Stories
Imagine a wizard who sends different commands to various magical creatures to perform tasks; the wizard represents the invoker, and the creatures are the receivers.
Memory Tools
C-R-E: Command, Receiver, Executed - Recall the key roles in the Command Pattern.
Acronyms
C.O.D.E
Command Object Decouples Execution.
Flash Cards
Glossary
- Command Pattern
A behavioral design pattern that encapsulates a request as an object, allowing for parameterization of clients with different requests.
- Command Interface
An interface that declares a method for executing commands.
- Concrete Command
A concrete implementation of a command that defines the binding between a Receiver and an action.
- Receiver
The object that performs the action associated with the command.
Reference links
Supplementary resources to enhance your learning experience.