Slides from a presentation I gave on Cocoa Design Patterns - software design patterns that are suitable and common within Apple's Cocoa and Cocoa Touch frameworks using Objective C
1. Cocoa Design
Patterns
Stewart Gleadow
February 2010
2.
3. Timeline
1976 1980 1986 1988 1996 2000 2007 2011
Apple iPhone
Original Objective C
acquires mania
Smalltalk
NeXT
NeXT
& OS X Mac
Commerically
NeXTSTEP & app
licensed
Cocoa store
Smalltalk
Objective C 2.0
23. More?
• Cocoa Design Patterns, by Erik M.
Buck, Donald A.Yacktman
• (Advanced) Cocoa Programming
for Mac OS X - Aaron Hillegass
• Apple Developer Docs (http://
developer.apple.com)
• CocoaHeads (http://
groups.google.com/group/
cocoaheadsau)
24.
25. Stewart Gleadow
Thoughtworks
sgleadow@thoughtworks.com
@stewgleadow
Editor's Notes
- cocoa / objective C: timeline and history\n- Objective C language primer (not too much syntax) -> get a feel for language\n- look at common design patterns in the cocoa world\n
relevant to Objective C, Cocoa and Design Patterns\n- smalltalk, graphical OS, popup menus\n
Inventors of ObjC worked with Smalltalk in the early 80s\nObjective C aimed at adding some of Smalltalk’s capabilities to C\nThere is a GNU project for the OpenStep standard (but mainly just Apple)\n\n
- strict superset of C -> may only write UI layer in Objective C\n- adds SmallTalk style messaging (over C++ block style)\n- runtime small in size, but some overhead\n- performSelector and respondsToSelector\n- uses header files, C structs, function pointers\n
- multiple arguments (~ named)\n- strings different for compatibility with C\n
Native libraries for OS X, written in Objective C\nUsually built with XCode and Interface Builder\nBridges for Java/Python/Ruby and a few others\n
Well thought out and designed library, plenty of design patterns.\nOften not well used.\n
Apple often uses their own names\n\n
\n
- common functionality implemented once\n- allow client code to modify behaviour\n“don’t call us, we’ll call you”\n- delegate: like a single observer, using a protocol\n- set target and action for events, like button tap\n
\n
\n
\n
\n
\n
\n
\n
- implementing the default NSEnumerable protocol means can be used by the inbuilt for loops\n- blocks in Objective C 2.0\n
- message passing makes this fairly easy to implement\nrespondsToSelector\nperformSelector\n
- Objective C strict superset of C\n- wrapping primitive objects, no auto-boxing\n- access to Carbon APIs\n
- can be used and abused\n- Cocoa seems to use it fairly well\n- iPhone users tent to abuse it -> easy global storage\n\n
- observer/command\n- becomes easier because of KVC/KVO and dynamic message passing\n- debugging? clarity?\n