Tutorials Design Patterns Mastery

Command Pattern: Implementing Undo/Redo & Queueing

On this page

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."

Design Patterns Mastery
Course syllabus
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)
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details