Pro Objective-C Design Patterns for ios Carlo Chung
Contents Contents at a Glance About the Author About the Technical Reviewer Acknowledgments Preface x xi xii xiii Part I: Getting Your Feet Wet 1 Chapter 1: Hello, Design Patterns! 3 What This Book Is 3 What You Need Before You Can Begin 4 What You Need to Know Before You Can Begin 4 Design Deja-vu 5 The Origin of Design Patterns Model, View, and Controller (MVC) 6 - Encapsulating Data and Basic Behaviors In Model Objects 6 Presenting Information to the User with View Objects 7 Tying up the Model to the View with Controller Objects 7 MVC as a Compound Design Pattern 7 Issues That Can AffectYour Design 9 Programming To An Interface, Not An Implementation 9 protocol vs. Abstract Base Class (ABC) 10 Object Composition vs. Class Inheritance 11 Objects and Classes Used in this Book 13 Class Diagram 13 Object Diagram 17 How the Patterns are Organized 18 Summary 18 Chapter 2: A Case Study: Designing an App 19 Conceptualizing the Ideas 20 Designing the Look-and-Feel 21 Architectural Design 23 View Management 24 V
a CONTENTS Scribble Representation 27 Representation of a Saved Scribble 33 User Operations Reviewing the Patterns Used 44 Summary 44 Part II: Object Creation 45 Chapter 3: Prototype 47 What Is the Prototype Pattern? 48 When Would You Use the Prototype Pattern? 48 Shallow Copying vs. Deep Copying 49 Using Object Copying in the Cocoa Touch Framework 51 Implementing the Copy Method for the Mark Aggregate 52 Using a Cloned Mark As a "Pattern Template" 60 Summary 62 Chapter 4: Factory Method 63 What Is the Factory Method Pattern? 63 When Would You Use the Factory Method? 64 Why Is It a Safe Approach to Creating Objects? 65 Generating Different Canvases in TouchPainter 65 Using Canvases 70 Using Factory Methods Within the Cocoa Touch Framework 71 Summary 72 Chapter 5: Abstract Factory 73 Applying the Abstract Factory to the TouchPainter App 75 Using the Abstract Factory Within the Cocoa Touch Framework 81 Summary 84 Chapter 6: Builder 85 What Is the Builder Pattern? 85 When Would You Use the Builder Pattern? 87 Building Characters in a Chasing Game 88 Summary 96 Chapter 7: Singleton 97 What Is the Singleton Pattern? 97 When Would You Use the Singleton Pattern? 98 Implementing a Singleton in Objective-C 99 Subclassing a Singleton 103 Thread Safety 103 Using Singletons in the Cocoa Touch Framework 104 Using the UlApplication Class 104 Using the UlAccelerometer Class 104 Using the NSFileManager Class 104 Summary 105 Part III: Interface Adaptation 107 Chapter 8: Adapter 109 What Is the Adapter Pattern? 109 When Would You Use the Adapter Pattern? 112 37 Vi
CONTENTS So You Know Delegation 112 Implementing the Adapter Pattern with Objective-C Protocol 113 Designing and Implementing a Stroke Color Changing Mechanism 114 Implementing the Adapter Pattern with Objective-C Blocks in ios 4 119 Declaring a Block Reference 120 Creating a Block 120 Using a Block As an Adapter 121 Summary 125 Chapter 9: Bridge 127 What Is the Bridge Pattern? 127 When Would You Use the Bridge Pattern? 129 Creating a Virtual Emulator for ios 129 Summary 135 Chapter 10: Fagade 137 What Is the Fagade Pattern? 137 When Would You Use the Fagade Pattern? 138 Providing a Simplified Interface to a Set of Interfaces in the Subsystem 139 Using the Fagade Pattern in the TouchPainter App 142 Summary 144 Part IV: Decoupling of Objects 145 Chapter 11: Mediator 147 What Is the Mediator Pattern? 148 When Would You Use the Mediator Pattern? 149 Managing View Transition in the TouchPainter App 149 The Troubles of Modifying the Transition Logic 152 Centralizing the Flow of the Ul Traffic 154 Using the CoordinatingController in the Interface Builder 159 Summary 163 Chapter 12: Observer 165 What Is the Observer Pattern? 165 When Would You Use the Observer Pattern? 167 Using the Observer Pattern in Model-View-Controller 168 Using the Observer Pattern in the Cocoa Touch Framework 168 Notifications 168 Key-Value Observing 170 Updating Strokes on the CanvasView in TouchPainter 171 Summary 181 Part V: Abstract Collection 183 Chapter 13: Composite 185 What Is the Composite Pattern? 185 When Would You Use the Composite Pattern? 188 Understanding the Use of Mark in TouchPainter 188 Using Composites in the Cocoa Touch Framework 197 Summary 199 Chapter 14: Iterator 201 What Is the Iterator Pattern? 202 Vll
CONTENTS When Would You Use the Iterator Pattern? 204 Using Iterators in the Cocoa Touch Framework 204 NSEnumerator 205 Block-Based Enumeration 205 Fast Enumeration 206 Internal Enumeration 207 Enumerating Vertices of a Scribble 208 Enumerating Vertices of a Scribble (Internally) 214 Summary 216 Part VI: Behavioral Extension 219 Chapter 15: Visitor 221 What is the Visitor Pattern? 221 When Would You Use the Visitor Pattern 224 Rendering Marks in TouchPainter with Visitors 224 What Else Can A Visitor Do? 231 Can't I Just Use Categories Instead? 231 Summary 232 Chapter 16: Decorator 233 What Is the Decorator Pattern? 233 When Would You Use the Decorator Pattern? 235 Changing the "Skin" vs. "Guts" of an Object 235 Creating Image Filters for Ullmage 236 Implementing Decorators with True Subclasses 237 Implementing Decorators with Categories 246 Summary 252 Chapter 17: Chain of Responsibility 253 What Is the Chain of Responsibility Pattern? 254 When Would You Use the Chain of Responsibility Pattern? 255 Using the Chain of Responsibility Pattern in an RPG 255 Summary 262 Part Vli: Algorithm Encapsulation 263 Chapter 18: Template Method 265 What Is the Template Method Pattern? 265 When Would You Use the Template Method? 266 Using the Template Method to Make a Sandwich 267 Ensuring That the Template Method Is Working 273 Adding an Extra Step to the Template Method 275 Using the Template Method in the Cocoa Touch Framework 278 Custom Drawing in the UlView Class 278 Other Template Method Implementations in Cocoa Touch 279 Summary 279 Chapter 19: Strategy 281 What Is the Strategy Pattern? 281 When Would You Use the Strategy Pattern? 283 Applying Validation Strategies in UlTextField 283 Summary 291 viii
CONTENTS Chapter 20: Command 293 What Is the Command Pattern? 293 When Would You Use the Command Pattern? 294 Using the Command Pattern in the Cocoa Touch Framework 295 NSInvocation Objects 295 NSUndoManager 296 Implementing Undo/Redo in TouchPainter 297 Implementing Drawing/Undrawing with NSUndoManager 298 Home-Brewing Your Own Drawing/Undrawing Infrastructure 303 Allowing the User to Activate Undo/Redo 311 What Else Can a Command Do? 312 Summary 312 Part VIII: Performance and Object Access 313 Chapter 21: Flyweight 315 What Is the Flyweight Pattern? 315 When Would You Use the Flyweight Pattern? 317 Creating a Hundred-Flower Pool 318 Designing and Implementing Sharable Flowers 319 Summary 326 Chapter 22: Proxy 327 What Is the Proxy Pattern? 328 When Would You Use the Proxy Pattern? 329 Lazy-Loading an Image with a Virtual Proxy 329 Designing and Implementing a Scribble Thumbnail View 331 Using a Proxy Pattern in the Cocoa Touch Framework 336 Summary 338 Part IX: State Of Object 339 Chapter 23: Memento 341 What's the Memento Pattern? 341 When Would You Use the Memento Pattern? 343 Using the Memento Pattern in TouchPainter 343 Saving a Scribble 344 Restoring a Scribble 34*> Designing and Implementing ScribbleMemento 347 The Memento Pattern in the Cocoa Touch Framework 357 Summary 359 Index 361 Ix