Book Image

Swift 2 Design Patterns

By : Julien Lange
Book Image

Swift 2 Design Patterns

By: Julien Lange

Overview of this book

Table of Contents (15 chapters)
Swift 2 Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The command pattern


The concept behind this pattern is to transform a request into an object in order to facilitate some actions, such as undo/redo, insertion into a queue, or tracking of the request.

Roles

The command pattern creates distance between the client that requests an operation and the object that can perform it. The request is encapsulated into an object. This object contains a reference to the receiver who will effectively execute the operation.

The real operation is managed by the receiver and the command is like an order; it only contains a reference to the invoker, the object that will perform the action and an execute function will call the real operation on the worker.

This pattern allows the following features:

  • Sending requests to different receivers

  • Queuing, logging, and rejecting requests

  • Undoable actions (the execute method can memorize the state and allows you to go back to that state)

  • Encapsulate a request in an object

  • Allows the parameterization of clients with different...