🎉 Hooray! After 3 years of work, I've finally released the ebook on design patterns! Check it out »

Command in Swift

Command is behavioral design pattern that converts requests or simple operations into objects.

The conversion allows deferred or remote execution of commands, storing command history, etc.

Usage of the pattern in Swift



Usage examples: The Command pattern is pretty common in Swift code. Most often it’s used as an alternative for callbacks to parameterizing UI elements with actions. It’s also used for queueing tasks, tracking operations history, etc.

Identification: The Command pattern is recognizable by behavioral methods in an abstract/interface type (sender) which invokes a method in an implementation of a different abstract/interface type (receiver) which has been encapsulated by the command implementation during its creation. Command classes are usually limited to specific actions.

Command in Other Languages

Design Patterns: Command in Java Design Patterns: Command in C# Design Patterns: Command in PHP Design Patterns: Command in Python Design Patterns: Command in Ruby Design Patterns: Command in TypeScript