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 visitor pattern


In this section, we will talk about the visitor pattern, which allows us to separate data and their associated treatments.

Roles

The visitor pattern allows us to externalize and centralize the actions that must be executed on object; these objects cannot have any links between them.

These actions will not be implemented in the class of the objects but in external classes.

So, this allows us to add any action in an external class, even a concrete visitor that implements IVisitor.

This pattern can be used when:

  • We need to add functionalities to a group of classes without weighing down these classes

  • A group of classes have a fixed structure and we need to add some functionalities to them without modifying their interface

The visitor pattern must be applied and used when you need to perform operations on objects of a collection that do not share a common base class or conform to a common protocol.

Design

The following diagram shows us how objects and treatments are separated. Treatments...