Mac/iOS Design Patterns

8,909 views
8,543 views

Published on

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

1 Comment
20 Likes
Statistics
Notes
No Downloads
Views
Total views
8,909
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
287
Comments
1
Likes
20
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. Design PatternsRobert Brown@robby_brown
    2. 2. What is a Design Pattern?After working on related problems, patterns oftenappear in the solutions.Formalized description of best practice.
    3. 3. Singleton
    4. 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. 5. Creating a Singleton https://gist.github.com/1116294
    6. 6. Related PatternsAbstract FactoryPlug-in RegistryNotification Center
    7. 7. Delegate
    8. 8. 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.
    9. 9. Delegate Interaction UIApplication UIApplication Delegate
    10. 10. Delegate Interaction UITableView UITableView UITableView DataSource Delegate
    11. 11. 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.
    12. 12. Related PatternsData SourceBlock DelegationCallbackChain of ResponsibilityStateFlyweight
    13. 13. Block Delegation
    14. 14. 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.
    15. 15. Block Delegation Interaction Object Block 1 Block 2
    16. 16. 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.
    17. 17. Related PatternsDelegateCallback
    18. 18. Model View Controller
    19. 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 swapout views when needed.
    20. 20. Model View ControllerInterface View Controller Xcode Builder Model Core Data
    21. 21. GotchasSome implementations of MVC have the View andModel directly connected. This can cause infiniteupdate cycles.
    22. 22. Related PatternsObserver
    23. 23. Layers
    24. 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. 25. Core Data Layers Application NSManagedObject Subclass NSManagedObject Context NSManagedObject NSPersistentStoreCoordinator NSManagedObjectModel NSPersistentStore XML SQLite Binary In Memory Custom
    26. 26. 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.
    27. 27. Façade
    28. 28. 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.
    29. 29. External External External Object Object Object FaçadeObject ObjectObject Object Object
    30. 30. Related PatternsClass clusterLayerProxy
    31. 31. Observer
    32. 32. 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).
    33. 33. GotchasDon’t forget to remove an object as an observer whenthat object is deallocated.
    34. 34. Related PatternsModel View ControllerNotification Center
    35. 35. Proxy
    36. 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 proxyNSProxy can be used to make any kind of proxyobject.
    37. 37. Lazy Loading Proxy Object Proxy Photo
    38. 38. Distributed Proxy RemoteObject Proxy Network Object
    39. 39. Immutable Proxy Mutable Object Proxy Object
    40. 40. GotchasIf you use NSProxy, you need to be very familiar withmessage forwarding.
    41. 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
    42. 42. Questions?

    ×