SlideShare a Scribd company logo
Design Patterns
Robert Brown
@robby_brown
What is a Design Pattern?


After working on related problems, patterns often
appear in the solutions.
Formalized description of best practice.
Singleton
What is a Singleton?

 A singleton is an object that only has one instance for
 the lifetime of the application.
 Some things are not safe to have more than one
 instance running. For example, an updater.
 Objective-C classes are singletons by design. You can
 take advantage of this when creating singletons.
Creating a Singleton



 https://gist.github.com/1116294
Related Patterns


Abstract Factory
Plug-in Registry
Notification Center
Delegate
What is a Delegate?

A delegate defines the behavior of a generic class.
This keeps implementation specific behavior out of the
generic class.
Data sources are similar to delegates, except they
define content rather than behavior.
UITableViews use both a delegate and a data source.
Delegate Interaction

            UIApplication




                            UIApplication
                              Delegate
Delegate Interaction

                     UITableView




       UITableView                 UITableView
       DataSource                   Delegate
Gotchas


Use “assign” instead of “retain” to avoid retain cycles.
  When using assign, be sure to nullify the delegate
  reference when deallocating the object.
Use “weak” instead of “assign” when using ARC.
Related Patterns
Data Source
Block Delegation
Callback
Chain of Responsibility
State
Flyweight
Block Delegation
What is Block Delegation?
Similar to a delegate object.
Rather than give an object that has one or more
methods defined, give a block that has the
implementation of one method.
Rule of thumb: If you have three or more delegate
methods, use a delegate. Otherwise, use block
delegation.
  Sometimes it’s appropriate to break this rule.
Block Delegation Interaction

                 Object




       Block 1            Block 2
Gotchas

Watch out for retain cycles, particularly with “self”
  __block __unsafe_unretained id selfRef = self;
  __block __weak id selfRef = self;
Don’t forget to copy your blocks. They are created on
the stack.
Related Patterns


Delegate
Callback
Model View Controller
What is MVC?
A strict organization of classes into their different roles.
  Model: Any underlying data.
  View: The graphical presentation of the data.
  Controller: Coordinates the interactions between the
  views and model.
Keeps programs modular and makes it easy to swap
out views when needed.
Model View Controller

Interface   View             Controller   Xcode
 Builder



                     Model



                   Core Data
Gotchas


Some implementations of MVC have the View and
Model directly connected. This can cause infinite
update cycles.
Related Patterns
Observer
Layers
What is the Layer Pattern?

 Modules of a program can be organized like a stack of
 layers.
 Strict Layering: A layer can only use the layer directly
 below it.
 Non-strict Layering: A layer can use any layer below it.
Core Data Layers
                       Application

                           NSManagedObject Subclass
  NSManagedObject
     Context
                                NSManagedObject

 NSPersistentStoreCoordinator   NSManagedObjectModel

                    NSPersistentStore

 XML       SQLite      Binary   In Memory   Custom
Gotchas

Occasionally you may find a need to call up a layer. To
do this, you will need to use dependency inversion.
Dependency inversion: A lower layer defines an
interface to interact with other layers. This is often used
with notifications. An upper layer can register itself to
receive these notifications through the defined
interface.
Façade
What is a Façade?

A façade is a single entry point to a complex collection
of classes.
Makes interaction with complex collections easier and
less dependent on internal implementation.
This is the basis of the layer pattern.
External   External   External
 Object     Object     Object




           Façade




Object                Object




Object     Object     Object
Related Patterns


Class cluster
Layer
Proxy
Observer
What is an Observer?

Sometimes you want to know when an object
changes.
Often used with MVC.
  When a model object changes, you want to update
  the view(s) accordingly.
The observer pattern is built into every NSObject via
Key-Value Observing (KVO).
Gotchas



Don’t forget to remove an object as an observer when
that object is deallocated.
Related Patterns


Model View Controller
Notification Center
Proxy
What is a Proxy?
A proxy receives actions in behalf of another object.
Many types of proxies:
  Lazy loading proxy
  Distributed (network) proxy (NSDistantObject)
  Immutable proxy
NSProxy can be used to make any kind of proxy
object.
Lazy Loading Proxy


   Object   Proxy    Photo
Distributed Proxy


                           Remote
Object   Proxy   Network   Object
Immutable Proxy


                    Mutable
   Object   Proxy   Object
Gotchas



If you use NSProxy, you need to be very familiar with
message forwarding.
Want to Learn More?


Design Patterns: Elements of Reusable Object-
Oriented Software
  Every developer should own this book!
Cocoa Design Patterns in the Apple Docs
Questions?

More Related Content

What's hot (20)

PPTX
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
PPTX
Headless fragments in Android
Ali Muzaffar
 
PDF
Paris Tech Meetup talk : Troubles start at version 1.0
Laurent Cerveau
 
PDF
Introduction to Akka
Knoldus Inc.
 
PPTX
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Sachintha Gunasena
 
PDF
Introduction to nodejs
James Carr
 
PPTX
Design Pattern - Singleton Pattern
Mudasir Qazi
 
KEY
CoffeeScript By Example
Christopher Bartling
 
KEY
Modern Java Concurrency
Ben Evans
 
PPTX
Module Ninja .JS
Александър Динков
 
PDF
Javascript classes and scoping
Patrick Sheridan
 
PPTX
Singleton Design Pattern - Creation Pattern
Seerat Malik
 
PDF
Introduction to Actor Model and Akka
Yung-Lin Ho
 
KEY
Introduction to Actor Model and Akka
Yung-Lin Ho
 
PDF
Node.js Enterprise Middleware
Behrad Zari
 
PPTX
JavaScript Basics
Bhanuka Uyanage
 
PPTX
Design pattern proxy介紹 20130805
LearningTech
 
PPTX
Redux
Maulik Shah
 
PPTX
Constructors and Destructors
murugeswariSenthilku
 
PPTX
Lecture 5 javascript
Mujtaba Haider
 
Modern JavaScript Development @ DotNetToscana
Matteo Baglini
 
Headless fragments in Android
Ali Muzaffar
 
Paris Tech Meetup talk : Troubles start at version 1.0
Laurent Cerveau
 
Introduction to Akka
Knoldus Inc.
 
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Sachintha Gunasena
 
Introduction to nodejs
James Carr
 
Design Pattern - Singleton Pattern
Mudasir Qazi
 
CoffeeScript By Example
Christopher Bartling
 
Modern Java Concurrency
Ben Evans
 
Javascript classes and scoping
Patrick Sheridan
 
Singleton Design Pattern - Creation Pattern
Seerat Malik
 
Introduction to Actor Model and Akka
Yung-Lin Ho
 
Introduction to Actor Model and Akka
Yung-Lin Ho
 
Node.js Enterprise Middleware
Behrad Zari
 
JavaScript Basics
Bhanuka Uyanage
 
Design pattern proxy介紹 20130805
LearningTech
 
Constructors and Destructors
murugeswariSenthilku
 
Lecture 5 javascript
Mujtaba Haider
 

Viewers also liked (9)

PDF
Design Patterns in iOS
Yi-Shou Chen
 
PDF
iOS design patterns: blocks
Alessio Roberto
 
PDF
iOS Design Patterns
Bruno Guidolim
 
DOCX
Carlos lineo
elizandymarian
 
PDF
Peña - Analisis de datos multivariantes
Robert_Hooke
 
DOCX
Robert Hooke
varainca
 
PPT
Claudio Galeno
Dr. Jair García-Guerrero
 
PPTX
Robert hooke
Robert Hooke
 
DOCX
Cientificos y sus aportaciones a la bilogia
Bichitaa Stradlin
 
Design Patterns in iOS
Yi-Shou Chen
 
iOS design patterns: blocks
Alessio Roberto
 
iOS Design Patterns
Bruno Guidolim
 
Carlos lineo
elizandymarian
 
Peña - Analisis de datos multivariantes
Robert_Hooke
 
Robert Hooke
varainca
 
Robert hooke
Robert Hooke
 
Cientificos y sus aportaciones a la bilogia
Bichitaa Stradlin
 
Ad

Similar to Mac/iOS Design Patterns (20)

PPTX
Fundamental Design Patterns.pptx
JUNSHIN8
 
PPT
Pavel_Kravchenko_Mobile Development
Ciklum
 
PDF
Cocoa encyclopedia
Alex Ali
 
PDF
Design Patterns in Cocoa Touch
Eliah Nikans
 
PDF
Session 7 - Overview of the iOS7 app development architecture
Vu Tran Lam
 
PPTX
iOS Coding Best Practices
Jean-Luc David
 
PDF
iOS development best practices
Michal Juhas
 
PDF
02 objective-c session 2
Amr Elghadban (AmrAngry)
 
PDF
iOS (7) Workshop
Fábio Bernardo
 
PPT
JAVA design patterns and Basic OOp concepts
Rahul Malhotra
 
PPTX
Design pattern-presentation
Rana Muhammad Asif
 
PDF
Task 2 - Educational Article – Model View Controller (MVC)
Shubham Goenka
 
PDF
Iphone programming: Core Data Tutorial for iOS
Kenny Nguyen
 
PDF
Introduction of Xcode
Dhaval Kaneria
 
PDF
Qardio experience with Core Data
Dmitrii Ivanov
 
PPT
ios basics
Muthu Sabarinathan
 
PPTX
iOS app dev Training - Session1
Hussain Behestee
 
PPTX
MVx patterns in iOS (MVC, MVP, MVVM)
Yaroslav Voloshyn
 
PDF
Cocoa Heads Tricity - Design Patterns
Maciej Burda
 
PPT
Design patterns
mudabbirwarsi
 
Fundamental Design Patterns.pptx
JUNSHIN8
 
Pavel_Kravchenko_Mobile Development
Ciklum
 
Cocoa encyclopedia
Alex Ali
 
Design Patterns in Cocoa Touch
Eliah Nikans
 
Session 7 - Overview of the iOS7 app development architecture
Vu Tran Lam
 
iOS Coding Best Practices
Jean-Luc David
 
iOS development best practices
Michal Juhas
 
02 objective-c session 2
Amr Elghadban (AmrAngry)
 
iOS (7) Workshop
Fábio Bernardo
 
JAVA design patterns and Basic OOp concepts
Rahul Malhotra
 
Design pattern-presentation
Rana Muhammad Asif
 
Task 2 - Educational Article – Model View Controller (MVC)
Shubham Goenka
 
Iphone programming: Core Data Tutorial for iOS
Kenny Nguyen
 
Introduction of Xcode
Dhaval Kaneria
 
Qardio experience with Core Data
Dmitrii Ivanov
 
ios basics
Muthu Sabarinathan
 
iOS app dev Training - Session1
Hussain Behestee
 
MVx patterns in iOS (MVC, MVP, MVVM)
Yaroslav Voloshyn
 
Cocoa Heads Tricity - Design Patterns
Maciej Burda
 
Design patterns
mudabbirwarsi
 
Ad

More from Robert Brown (12)

PDF
High level concurrency
Robert Brown
 
PDF
Data Source Combinators
Robert Brown
 
PDF
Elixir
Robert Brown
 
PDF
Reactive Cocoa
Robert Brown
 
PDF
UIKit Dynamics
Robert Brown
 
PDF
iOS State Preservation and Restoration
Robert Brown
 
KEY
Anti-Patterns
Robert Brown
 
KEY
Pragmatic blocks
Robert Brown
 
KEY
Grand Central Dispatch Design Patterns
Robert Brown
 
KEY
Grand Central Dispatch
Robert Brown
 
KEY
Core Data
Robert Brown
 
KEY
Quick Look for iOS
Robert Brown
 
High level concurrency
Robert Brown
 
Data Source Combinators
Robert Brown
 
Elixir
Robert Brown
 
Reactive Cocoa
Robert Brown
 
UIKit Dynamics
Robert Brown
 
iOS State Preservation and Restoration
Robert Brown
 
Anti-Patterns
Robert Brown
 
Pragmatic blocks
Robert Brown
 
Grand Central Dispatch Design Patterns
Robert Brown
 
Grand Central Dispatch
Robert Brown
 
Core Data
Robert Brown
 
Quick Look for iOS
Robert Brown
 

Recently uploaded (20)

PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 

Mac/iOS Design Patterns

  • 2. What is a Design Pattern? After working on related problems, patterns often appear in the solutions. Formalized description of best practice.
  • 4. What is a Singleton? A singleton is an object that only has one instance for the lifetime of the application. Some things are not safe to have more than one instance running. For example, an updater. Objective-C classes are singletons by design. You can take advantage of this when creating singletons.
  • 5. Creating a Singleton https://gist.github.com/1116294
  • 6. Related Patterns Abstract Factory Plug-in Registry Notification Center
  • 8. What is a Delegate? A delegate defines the behavior of a generic class. This keeps implementation specific behavior out of the generic class. Data sources are similar to delegates, except they define content rather than behavior. UITableViews use both a delegate and a data source.
  • 9. Delegate Interaction UIApplication UIApplication Delegate
  • 10. Delegate Interaction UITableView UITableView UITableView DataSource Delegate
  • 11. Gotchas Use “assign” instead of “retain” to avoid retain cycles. When using assign, be sure to nullify the delegate reference when deallocating the object. Use “weak” instead of “assign” when using ARC.
  • 12. Related Patterns Data Source Block Delegation Callback Chain of Responsibility State Flyweight
  • 14. What is Block Delegation? Similar to a delegate object. Rather than give an object that has one or more methods defined, give a block that has the implementation of one method. Rule of thumb: If you have three or more delegate methods, use a delegate. Otherwise, use block delegation. Sometimes it’s appropriate to break this rule.
  • 15. Block Delegation Interaction Object Block 1 Block 2
  • 16. Gotchas Watch out for retain cycles, particularly with “self” __block __unsafe_unretained id selfRef = self; __block __weak id selfRef = self; Don’t forget to copy your blocks. They are created on the stack.
  • 19. What is MVC? A strict organization of classes into their different roles. Model: Any underlying data. View: The graphical presentation of the data. Controller: Coordinates the interactions between the views and model. Keeps programs modular and makes it easy to swap out views when needed.
  • 20. Model View Controller Interface View Controller Xcode Builder Model Core Data
  • 21. Gotchas Some implementations of MVC have the View and Model directly connected. This can cause infinite update cycles.
  • 24. What is the Layer Pattern? Modules of a program can be organized like a stack of layers. Strict Layering: A layer can only use the layer directly below it. Non-strict Layering: A layer can use any layer below it.
  • 25. Core Data Layers Application NSManagedObject Subclass NSManagedObject Context NSManagedObject NSPersistentStoreCoordinator NSManagedObjectModel NSPersistentStore XML SQLite Binary In Memory Custom
  • 26. Gotchas Occasionally you may find a need to call up a layer. To do this, you will need to use dependency inversion. Dependency inversion: A lower layer defines an interface to interact with other layers. This is often used with notifications. An upper layer can register itself to receive these notifications through the defined interface.
  • 28. What is a Façade? A façade is a single entry point to a complex collection of classes. Makes interaction with complex collections easier and less dependent on internal implementation. This is the basis of the layer pattern.
  • 29. External External External Object Object Object Façade Object Object Object Object Object
  • 32. What is an Observer? Sometimes you want to know when an object changes. Often used with MVC. When a model object changes, you want to update the view(s) accordingly. The observer pattern is built into every NSObject via Key-Value Observing (KVO).
  • 33. Gotchas Don’t forget to remove an object as an observer when that object is deallocated.
  • 34. Related Patterns Model View Controller Notification Center
  • 35. Proxy
  • 36. What is a Proxy? A proxy receives actions in behalf of another object. Many types of proxies: Lazy loading proxy Distributed (network) proxy (NSDistantObject) Immutable proxy NSProxy can be used to make any kind of proxy object.
  • 37. Lazy Loading Proxy Object Proxy Photo
  • 38. Distributed Proxy Remote Object Proxy Network Object
  • 39. Immutable Proxy Mutable Object Proxy Object
  • 40. Gotchas If you use NSProxy, you need to be very familiar with message forwarding.
  • 41. Want to Learn More? Design Patterns: Elements of Reusable Object- Oriented Software Every developer should own this book! Cocoa Design Patterns in the Apple Docs

Editor's Notes