0Pricing
Clean Architecture & Design Patterns in Practice · Lesson

Command and Iterator Patterns

Encapsulate requests as objects with Command and traverse collections with Iterator.

What is the Command Pattern?

The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object. This object contains all information about the request, including the method to call and the parameters it needs.

Think of it like putting an instruction into a package. You can then pass this package around, queue it, or even undo it later, without the sender needing to know how the instruction is carried out.

Core Components of Command

To understand the Command Pattern, let's look at its key players:

  • Command: An interface or abstract class declaring an execute() method.
  • ConcreteCommand: Implements the Command interface, binding a specific Receiver object to an action.
  • Receiver: The object that performs the actual work. It knows how to carry out the operation.
  • Invoker: Asks the command to carry out its request. It doesn't know the specifics of the operation or the receiver.
  • Client: Creates a ConcreteCommand object and sets its Receiver.

All lessons in this course

  1. Observer and Strategy Patterns
  2. Command and Iterator Patterns
  3. Template Method and State Patterns
  4. Mediator and Chain of Responsibility
← Back to Clean Architecture & Design Patterns in Practice