Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift 2 Design Patterns

You're reading from   Swift 2 Design Patterns Build robust and scalable iOS and Mac OS X game applications

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781785887611
Length 224 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Julien Lange Julien Lange
Author Profile Icon Julien Lange
Julien Lange
Arrow right icon
View More author details
Toc

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image