Design Patterns Mastery
Lesson 17 of 30 57% of course

Command Pattern: Implementing Undo/Redo & Queueing

16 · 8 min · 5/23/2026

Sign in to track progress and bookmarks.

The Command Pattern

The Command Pattern turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as method arguments, delay or queue a request's execution, and support undoable operations.

1. Decoupling Sender and Receiver

Normally, a button calls Database.Save(). If you use the Command pattern, the button calls Command.Execute(). The button has no idea what the command actually does, making it perfectly reusable.

public interface ICommand 
{
    void Execute();
    void Undo(); // The secret to Undo/Redo!
}

public class SaveUserCommand : ICommand 
{
    private readonly User _user;
    public void Execute() => _db.Save(_user);
    public void Undo() => _db.Delete(_user.Id);
}

2. Implementation: The Command History

By storing executed commands in a Stack<ICommand>, you can implement a global Undo feature. When the user hits Ctrl+Z, you pop the last command and call its .Undo() method.

4. Interview Mastery

Q: "What is the difference between Strategy and Command?"

Architect Answer: "The **Strategy** pattern is used to change *How* an object does something (it’s an algorithm). The **Command** pattern is used to change *What* an object does (it’s a request). You use Strategy at the start of a task to pick a path; you use Command to store that task so you can run it later, log it, or undo it."

Test your knowledge

Quizzes linked to this course—pass to earn certificates.

Browse all quizzes
Design Patterns Mastery

On this page

1. Decoupling Sender and Receiver 2. Implementation: The Command History 4. Interview Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)