• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mac/iOS Design Patterns
 

Mac/iOS Design Patterns

on

  • 6,755 views

Getting started with Mac/iOS can be hard. This presentation helps you learn the common design patterns used by Cocoa and Cocoa Touch.

Getting started with Mac/iOS can be hard. This presentation helps you learn the common design patterns used by Cocoa and Cocoa Touch.

Statistics

Views

Total Views
6,755
Views on SlideShare
6,753
Embed Views
2

Actions

Likes
13
Downloads
194
Comments
1

2 Embeds 2

http://elearning.mdis.edu.sg 1
http://www.verious.com 1

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Great presentation!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Mac/iOS Design Patterns Mac/iOS Design Patterns Presentation Transcript

  • Design PatternsRobert Brown@robby_brown
  • What is a Design Pattern?After working on related problems, patterns oftenappear 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 PatternsAbstract FactoryPlug-in RegistryNotification Center
  • Delegate
  • What is a Delegate?A delegate defines the behavior of a generic class.This keeps implementation specific behavior out of thegeneric class.Data sources are similar to delegates, except theydefine 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
  • GotchasUse “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 PatternsData SourceBlock DelegationCallbackChain of ResponsibilityStateFlyweight
  • Block Delegation
  • What is Block Delegation?Similar to a delegate object.Rather than give an object that has one or moremethods defined, give a block that has theimplementation of one method.Rule of thumb: If you have three or more delegatemethods, use a delegate. Otherwise, use blockdelegation. Sometimes it’s appropriate to break this rule.
  • Block Delegation Interaction Object Block 1 Block 2
  • GotchasWatch 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 onthe stack.
  • Related PatternsDelegateCallback
  • 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 swapout views when needed.
  • Model View ControllerInterface View Controller Xcode Builder Model Core Data
  • GotchasSome implementations of MVC have the View andModel directly connected. This can cause infiniteupdate cycles.
  • Related PatternsObserver
  • 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
  • GotchasOccasionally you may find a need to call up a layer. Todo this, you will need to use dependency inversion.Dependency inversion: A lower layer defines aninterface to interact with other layers. This is often usedwith notifications. An upper layer can register itself toreceive these notifications through the definedinterface.
  • Façade
  • What is a Façade?A façade is a single entry point to a complex collectionof classes.Makes interaction with complex collections easier andless dependent on internal implementation.This is the basis of the layer pattern.
  • External External External Object Object Object FaçadeObject ObjectObject Object Object
  • Related PatternsClass clusterLayerProxy
  • Observer
  • What is an Observer?Sometimes you want to know when an objectchanges.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 viaKey-Value Observing (KVO).
  • GotchasDon’t forget to remove an object as an observer whenthat object is deallocated.
  • Related PatternsModel View ControllerNotification 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 proxyNSProxy can be used to make any kind of proxyobject.
  • Lazy Loading Proxy Object Proxy Photo
  • Distributed Proxy RemoteObject Proxy Network Object
  • Immutable Proxy Mutable Object Proxy Object
  • GotchasIf you use NSProxy, you need to be very familiar withmessage 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?