Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Patterns

Rob Brown
October 13, 2011

Design Patterns

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

Rob Brown

October 13, 2011
Tweet

More Decks by Rob Brown

Other Decks in Programming

Transcript

  1. What is a Design Pattern? After working on related problems,

    patterns often appear in the solutions. Formalized description of best practice.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. Gotchas Some implementations of MVC have the View and Model

    directly connected. This can cause infinite update cycles.
  9. 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.
  10. Core Data Layers XML SQLite Binary In Memory Custom NSPersistentStore

    NSPersistentStoreCoordinator NSManagedObjectModel NSManagedObject Context NSManagedObject Application NSManagedObject Subclass
  11. 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.
  12. 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.
  13. 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).
  14. 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.
  15. Gotchas If you use NSProxy, you need to be very

    familiar with message forwarding.
  16. 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