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


The observer pattern is another behavioral pattern that is often used in networked system where a subject (the server) will notify some client. The iOS makes large use of this pattern through NSNotificationCenter object.

Roles

The observer pattern creates dependence between a subject and observer so that the observer is notified in order to update their state each time the subject is modified.

This composition means that observer does not need to ask the current state of the subject. They only need to register to its notifications.

This pattern can be used when:

  • A state modification inside the object needs to update other objects dynamically

  • An object wants to prevent other objects without the need to know the type (without having to be high coupled with them)

  • We do not want to merge two objects into one

Design

The following diagram represents the UML class diagram of the observer pattern:

Participants

This pattern is composed of the following participants:

  • Subject: This defines...

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