You are previewing Pro Objective-C Design Patterns for iOS.
O'Reilly logo
Pro Objective-C Design Patterns for iOS

Book Description

It's time to capitalize on your mastery of Cocoa with Pro Objective-C Design Patterns for iOS. You've developed apps that impressed and performed, and now you're ready to jump into development practices that will leave you with more effective, efficient, and professional level apps. This book is the element you need to make the jump from journeyman to master.

All too often, developers grind through building good apps on willpower and a vigorous focus on code development, leaving them unaware of and unable to benefit from the underlying structural and functional design patterns.

Pro Objective-C Design Patterns for iOS will teach you those design patterns that have always been present at some level in your code, but were never recognized, acknowledged, or fully utilized. Implementation of specific pattern approaches will prove their value to any developer working in the iOS application arena. You'll learn to master classic patterns like singleton, abstract factory, chain of responsibility, and observer. You'll also discover less well-known but useful patterns like memento, composite, command, and mediator.

What you'll learn

  • The basic concepts of various design patterns

  • How to apply design patterns to your code based on different scenarios

  • How design patterns can strengthen your apps

Who this book is for

Any professional or aspiring iOS developer will find productivity, efficiency, and effectiveness of software development enhanced by the methods and practice delivered by Pro Objective-C Design Patterns for iOS.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Preface
  6. I. Getting Your Feet Wet
    1. 1. Hello, Design Patterns!
      1. 1.1. What This Book Is
      2. 1.2. What You Need Before You Can Begin
      3. 1.3. What You Need to Know Before You Can Begin
      4. 1.4. Design Déjà-vu
      5. 1.5. The Origin of Design Patterns - Model, View, and Controller (MVC)
        1. 1.5.1. Encapsulating Data and Basic Behaviors In Model Objects
        2. 1.5.2. Presenting Information to the User with View Objects
        3. 1.5.3. Tying up the Model to the View with Controller Objects
        4. 1.5.4. MVC as a Compound Design Pattern
      6. 1.6. Issues That Can Affect Your Design
        1. 1.6.1. Programming To An Interface, Not An Implementation
        2. 1.6.2. @protocol vs. Abstract Base Class (ABC)
        3. 1.6.3. Object Composition vs. Class Inheritance
      7. 1.7. Objects and Classes Used in this Book
        1. 1.7.1. Class Diagram
          1. 1.7.1.1. Protocol, Abstract Class, Concrete Class, and Category
          2. 1.7.1.2. Instantiation
          3. 1.7.1.3. Inheritance
          4. 1.7.1.4. Acquaintance
          5. 1.7.1.5. Aggregation
          6. 1.7.1.6. Pseudocode
        2. 1.7.2. Object Diagram
      8. 1.8. How the Patterns are Organized
      9. 1.9. Summary
    2. 2. A Case Study: Designing an App
      1. 2.1. Conceptualizing the Ideas
      2. 2.2. Designing the Look-and-Feel
      3. 2.3. Architectural Design
        1. 2.3.1. View Management
          1. 2.3.1.1. Transition from One View to Another
          2. 2.3.1.2. Using a Mediator to Coordinate View Transitions
        2. 2.3.2. Scribble Representation
          1. 2.3.2.1. "What" Can Be Drawn Onscreen?
          2. 2.3.2.2. Representing Marks with a Composite Structure
          3. 2.3.2.3. Drawing a Scribble
        3. 2.3.3. Representation of a Saved Scribble
          1. 2.3.3.1. Capturing the State of a Scribble
          2. 2.3.3.2. Restoring the State of a Scribble
        4. 2.3.4. User Operations
          1. 2.3.4.1. Browsing a List of Scribble Thumbnails
          2. 2.3.4.2. Undo/Redo Scribbling
          3. 2.3.4.3. Changing Stroke Colors and Sizes
          4. 2.3.4.4. Deleting the Current Scribble Onscreen
      4. 2.4. Reviewing the Patterns Used
      5. 2.5. Summary
  7. II. Object Creation
    1. 3. Prototype
      1. 3.1. What Is the Prototype Pattern?
      2. 3.2. When Would You Use the Prototype Pattern?
      3. 3.3. Shallow Copying vs. Deep Copying
      4. 3.4. Using Object Copying in the Cocoa Touch Framework
      5. 3.5. Implementing the Copy Method for the Mark Aggregate
      6. 3.6. Using a Cloned Mark As a "Pattern Template"
      7. 3.7. Summary
    2. 4. Factory Method
      1. 4.1. What Is the Factory Method Pattern?
      2. 4.2. When Would You Use the Factory Method?
      3. 4.3. Why Is It a Safe Approach to Creating Objects?
      4. 4.4. Generating Different Canvases in TouchPainter
        1. 4.4.1. Using Canvases
      5. 4.5. Using Factory Methods Within the Cocoa Touch Framework
      6. 4.6. Summary
    3. 5. Abstract Factory
      1. 5.1. Applying the Abstract Factory to the TouchPainter App
      2. 5.2. Using the Abstract Factory Within the Cocoa Touch Framework
      3. 5.3. Summary
    4. 6. Builder
      1. 6.1. What Is the Builder Pattern?
      2. 6.2. When Would You Use the Builder Pattern?
      3. 6.3. Building Characters in a Chasing Game
      4. 6.4. Summary
    5. 7. Singleton
      1. 7.1. What Is the Singleton Pattern?
      2. 7.2. When Would You Use the Singleton Pattern?
      3. 7.3. Implementing a Singleton in Objective-C
      4. 7.4. Subclassing a Singleton
      5. 7.5. Thread Safety
      6. 7.6. Using Singletons in the Cocoa Touch Framework
        1. 7.6.1. Using the UIApplication Class
        2. 7.6.2. Using the UIAccelerometer Class
        3. 7.6.3. Using the NSFileManager Class
      7. 7.7. Summary
  8. III. Interface Adaptation
    1. 8. Adapter
      1. 8.1. What Is the Adapter Pattern?
      2. 8.2. When Would You Use the Adapter Pattern?
      3. 8.3. So You Know Delegation
      4. 8.4. Implementing the Adapter Pattern with Objective-C Protocol
        1. 8.4.1. Designing and Implementing a Stroke Color Changing Mechanism
          1. 8.4.1.1. Defining SetStrokeColorCommandDelegate As a Target
          2. 8.4.1.2. Implementing SetStrokeCommand As a Client
          3. 8.4.1.3. Creating CommandSlider As an Adaptee
          4. 8.4.1.4. Using PaletteViewController As an Adapter
      5. 8.5. Implementing the Adapter Pattern with Objective-C Blocks in iOS 4
        1. 8.5.1. Declaring a Block Reference
        2. 8.5.2. Creating a Block
        3. 8.5.3. Using a Block As an Adapter
      6. 8.6. Summary
    2. 9. Bridge
      1. 9.1. What Is the Bridge Pattern?
      2. 9.2. When Would You Use the Bridge Pattern?
      3. 9.3. Creating a Virtual Emulator for iOS
      4. 9.4. Summary
    3. 10. Façade
      1. 10.1. What Is the Façade Pattern?
      2. 10.2. When Would You Use the Façade Pattern?
      3. 10.3. Providing a Simplified Interface to a Set of Interfaces in the Subsystem
      4. 10.4. Using the Façade Pattern in the TouchPainter App
      5. 10.5. Summary
  9. IV. Decoupling of Objects
    1. 11. Mediator
      1. 11.1. What Is the Mediator Pattern?
      2. 11.2. When Would You Use the Mediator Pattern?
      3. 11.3. Managing View Transition in the TouchPainter App
        1. 11.3.1. The Troubles of Modifying the Transition Logic
        2. 11.3.2. Centralizing the Flow of the UI Traffic
        3. 11.3.3. Using the CoordinatingController in the Interface Builder
      4. 11.4. Summary
    2. 12. Observer
      1. 12.1. What Is the Observer Pattern?
      2. 12.2. When Would You Use the Observer Pattern?
      3. 12.3. Using the Observer Pattern in Model-View-Controller
      4. 12.4. Using the Observer Pattern in the Cocoa Touch Framework
        1. 12.4.1. Notifications
        2. 12.4.2. Key-Value Observing
      5. 12.5. Updating Strokes on the CanvasView in TouchPainter
      6. 12.6. Summary
  10. V. Abstract Collection
    1. 13. Composite
      1. 13.1. What Is the Composite Pattern?
      2. 13.2. When Would You Use the Composite Pattern?
      3. 13.3. Understanding the Use of Mark in TouchPainter
      4. 13.4. Using Composites in the Cocoa Touch Framework
      5. 13.5. Summary
    2. 14. Iterator
      1. 14.1. What Is the Iterator Pattern?
      2. 14.2. When Would You Use the Iterator Pattern?
      3. 14.3. Using Iterators in the Cocoa Touch Framework
        1. 14.3.1. NSEnumerator
        2. 14.3.2. Block-Based Enumeration
        3. 14.3.3. Fast Enumeration
        4. 14.3.4. Internal Enumeration
      4. 14.4. Enumerating Vertices of a Scribble
        1. 14.4.1. Enumerating Vertices of a Scribble (Internally)
      5. 14.5. Summary
  11. VI. Behavioral Extension
    1. 15. Visitor
      1. 15.1. What is the Visitor Pattern?
      2. 15.2. When Would You Use the Visitor Pattern
      3. 15.3. Rendering Marks in TouchPainter with Visitors
      4. 15.4. What Else Can A Visitor Do?
      5. 15.5. Can't I Just Use Categories Instead?
      6. 15.6. Summary
    2. 16. Decorator
      1. 16.1. What Is the Decorator Pattern?
      2. 16.2. When Would You Use the Decorator Pattern?
      3. 16.3. Changing the "Skin" vs. "Guts" of an Object
      4. 16.4. Creating Image Filters for UIImage
        1. 16.4.1. Implementing Decorators with True Subclasses
        2. 16.4.2. Implementing Decorators with Categories
      5. 16.5. Summary
    3. 17. Chain of Responsibility
      1. 17.1. What Is the Chain of Responsibility Pattern?
      2. 17.2. When Would You Use the Chain of Responsibility Pattern?
      3. 17.3. Using the Chain of Responsibility Pattern in an RPG
      4. 17.4. Summary
  12. VII. Algorithm Encapsulation
    1. 18. Template Method
      1. 18.1. What Is the Template Method Pattern?
      2. 18.2. When Would You Use the Template Method?
      3. 18.3. Using the Template Method to Make a Sandwich
      4. 18.4. Ensuring That the Template Method Is Working
      5. 18.5. Adding an Extra Step to the Template Method
      6. 18.6. Using the Template Method in the Cocoa Touch Framework
        1. 18.6.1. Custom Drawing in the UIView Class
        2. 18.6.2. Other Template Method Implementations in Cocoa Touch
      7. 18.7. Summary
    2. 19. Strategy
      1. 19.1. What Is the Strategy Pattern?
      2. 19.2. When Would You Use the Strategy Pattern?
      3. 19.3. Applying Validation Strategies in UITextField
      4. 19.4. Summary
    3. 20. Command
      1. 20.1. What Is the Command Pattern?
      2. 20.2. When Would You Use the Command Pattern?
      3. 20.3. Using the Command Pattern in the Cocoa Touch Framework
        1. 20.3.1. NSInvocation Objects
        2. 20.3.2. NSUndoManager
          1. 20.3.2.1. Registering Undo Command Operations
      4. 20.4. Implementing Undo/Redo in TouchPainter
        1. 20.4.1. Implementing Drawing/Undrawing with NSUndoManager
          1. 20.4.1.1. Adding Methods for Generating Drawing/Undrawing Invocations
          2. 20.4.1.2. Adding Methods for Registering Undo/Redo Operations in NSUndoManager
          3. 20.4.1.3. Modifying Touch Event Handlers for Invocations
        2. 20.4.2. Home-Brewing Your Own Drawing/Undrawing Infrastructure
          1. 20.4.2.1. Designing a Command Infrastructure
          2. 20.4.2.2. Implementing the Command Classes
          3. 20.4.2.3. Modifying CanvasViewController for the Commands
        3. 20.4.3. Allowing the User to Activate Undo/Redo
      5. 20.5. What Else Can a Command Do?
      6. 20.6. Summary
  13. VIII. Performance and Object Access
    1. 21. Flyweight
      1. 21.1. What Is the Flyweight Pattern?
      2. 21.2. When Would You Use the Flyweight Pattern?
      3. 21.3. Creating a Hundred-Flower Pool
        1. 21.3.1. Designing and Implementing Sharable Flowers
          1. 21.3.1.1. Implementing the FlowerView Class
          2. 21.3.1.2. Implementing the FlowerFactory Class
          3. 21.3.1.3. How Are FlowerView Objects Shared?
          4. 21.3.1.4. Implementing Client Code
      4. 21.4. Summary
    2. 22. Proxy
      1. 22.1. What Is the Proxy Pattern?
      2. 22.2. When Would You Use the Proxy Pattern?
      3. 22.3. Lazy-Loading an Image with a Virtual Proxy
        1. 22.3.1. Designing and Implementing a Scribble Thumbnail View
          1. 22.3.1.1. Implementing the ScribbleThumbnailView Class
          2. 22.3.1.2. Implementing the ScribbleThumbnailViewImageProxy Class
      4. 22.4. Using a Proxy Pattern in the Cocoa Touch Framework
      5. 22.5. Summary
  14. IX. State Of Object
    1. 23. Memento
      1. 23.1. What's the Memento Pattern?
      2. 23.2. When Would You Use the Memento Pattern?
      3. 23.3. Using the Memento Pattern in TouchPainter
        1. 23.3.1. Saving a Scribble
        2. 23.3.2. Restoring a Scribble
        3. 23.3.3. Designing and Implementing ScribbleMemento
          1. 23.3.3.1. Implementing the ScribbleMemento Class
          2. 23.3.3.2. Modifying the Scribble Class
          3. 23.3.3.3. Putting Everything Together with a Caretaker
      4. 23.4. The Memento Pattern in the Cocoa Touch Framework
      5. 23.5. Summary