WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Behavioral Patterns

pattern · 12 MIN READ

Command

Command packages a request with the information needed to execute it. Supporting undo requires capturing the prior state or a valid inverse at execution time.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • Command packages a request with the information needed to execute it. Supporting undo requires capturing the prior state or a valid inverse at execution time.
  • An append command records the previous document text before applying the edit and restores that snapshot on undo.
  • Evaluate the tradeoff: Not every action has a reliable inverse. Network effects and external payments need compensating actions rather than pretending that undo erases history.

Useful first: Classes and Objects

The idea and when to recognize it

Command packages a request with the information needed to execute it. Supporting undo requires capturing the prior state or a valid inverse at execution time.

  • Requirement: An editor appends text and can undo the latest append.
  • Change to anticipate: Restore the exact prior text, including an initially empty document.
  • Design goal: An append command records the previous document text before applying the edit and restores that snapshot on undo.

Start with the concrete problem

An editor appends text and can undo the latest append.

Restore the exact prior text, including an initially empty document.

Direct implementationRevised design
Removing a fixed number of characters or clearing the document does not restore an arbitrary prior state.An append command records the previous document text before applying the edit and restores that snapshot on undo.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Not every action has a reliable inverse. Network effects and external payments need compensating actions rather than pretending that undo erases history.

Responsibilities and contracts

An append command records the previous document text before applying the edit and restores that snapshot on undo.

  • Editor: Owns document state. Public operations: text
  • AppendCommand: Captures a reversible request. Public operations: execute(), undo()

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A drawing tool can store move commands with the previous coordinates of each moved shape.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Not every action has a reliable inverse. Network effects and external payments need compensating actions rather than pretending that undo erases history.

Common mistake: Removing a fixed number of characters or clearing the document does not restore an arbitrary prior state.

  • Explain the choice: An append command records the previous document text before applying the edit and restores that snapshot on undo.
  • Challenge your choice: Not every action has a reliable inverse. Network effects and external payments need compensating actions rather than pretending that undo erases history.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.