Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How iOS Implements Undo/Redo with Command & Observer Patterns

How iOS Implements Undo/Redo with Command & Observer Patterns

This article explains how iOS keyboards achieve undo and redo functionality by combining the Command and Observer design patterns, illustrates their class structures, and shows how the NSUndoManager leverages these patterns to manage undo and redo stacks in Cocoa Touch applications.

Current keyboards—both system‑provided and third‑party—offer undo and redo features. Do you know which design patterns power them? They rely on the Command pattern together with the Observer pattern .

Simple Introduction

What is the Command (Command) pattern?

The Command pattern is a behavioral design pattern that encapsulates an operation, method call, or command into an object, which can be executed at an appropriate time.

Pattern type: Behavioral pattern

Definition: Encapsulate a request as an object.

Typical scenario: Like a guard ready to act on command—e.g., a menu button that triggers a specific action when clicked.

Class diagram:

Command pattern class diagram
Command pattern class diagram

The invoker holds a composite command class, and the command class references the command receiver.

What is the Observer (Observer) pattern?

Pattern type: Behavioral pattern

Definition: Defines a one‑to‑many relationship where a change in the subject (observable) automatically notifies observers, allowing them to update their state.

Typical scenario: Controllers observe model properties; when a property changes, the view updates. Keyboard input is observed and then displayed.

Class diagram:

Observer pattern class diagram
Observer pattern class diagram

The observable holds references to its observers.

Real‑World Use in the Cocoa Touch Framework

Apple provides NSUndoManager for undo/redo management, which consists of three main components:

Redo stack

Undo stack

Command object:

NSInvocation
NSUndoManager structure diagram
NSUndoManager structure diagram

When entering the undo stack, NSInvocation executes the undo command; when entering the redo stack, it executes the redo command.

To combine these patterns in your own implementation:

Add an NSInvocation execution object to the Command.

Include an observer list in the Command so that executing a command notifies its observers.

Create a manager class similar to NSUndoManager to handle the overall command list.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
design patternsMobile DevelopmentiOSObserver Patterncommand patternUndo/Redo

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion