03 / 11

Command Pattern

Difficulty: 6/10
encapsulation of actions, undo/redo, macro commands

The Command pattern encapsulates a request as an object, allowing parameterization of clients with different requests, queuing of requests, logging, and support for undoable operations.

The Command pattern turns a request into a stand-alone object that contains all information about the request. This transformation allows you to parameterize methods with different requests, delay or queue a request's execution, and support undoable operations. This pattern is fundamental to Redux (actions), command-line interfaces, and transaction systems. In frontend development, it's commonly used for undo/redo functionality, macro recording, and action dispatching.

Vanilla JavaScript Implementation
React Implementation (Redux pattern)
Angular Implementation

Scenario Questions

0-2 years experience

  1. 1We need to add a 'Save' button that can be disabled/enabled based on user input. How would you use the Command pattern to encapsulate the save action?
  2. 2If you have a simple text editor with Cut, Copy, Paste, how would you structure those operations using Command objects?
  3. 3What happens if you call execute on a command that hasn't been given a receiver?

2-5 years experience

  1. 1Our file‑processing service now needs to support undo for delete operations. Walk me through how you'd extend the existing Command implementation to add undo, and what pitfalls to watch out for.
  2. 2During a recent refactor we moved command creation into a factory, but now some commands are failing with NullPointerException. How would you debug the issue?
  3. 3We want to batch multiple commands into a single transaction. How would you design a macro command and handle partial failures?

5-8 years experience

  1. 1At scale we have thousands of user‑initiated commands per second. Discuss the trade‑offs of keeping command objects in memory versus serializing them to a queue for asynchronous processing.
  2. 2Our system needs to support distributed undo across microservices. How would you redesign the Command pattern to work in a distributed environment while preserving consistency?
  3. 3Explain how you would instrument command execution to collect latency metrics without breaking the pattern.

8+ years experience

  1. 1We are migrating a legacy monolith that uses ad‑hoc if‑else logic for actions into a Command‑based architecture across multiple teams. What migration strategy would you propose to minimize risk and ensure backward compatibility?
  2. 2How would you evolve the Command pattern to support versioned commands in a platform that must run old client binaries while rolling out new features?
  3. 3Discuss the long‑term maintenance implications of using Command objects for every user interaction in a large SaaS product, especially regarding code bloat and testability.

Follow-up Questions

  • What would you change if the command needed to be cancellable after being queued?
  • How does your design affect testability of individual actions?
  • Can you think of any drawbacks of using Command for simple UI events?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.