Successfully reported this slideshow.

Cocoa Design Patterns

6

Share

Upcoming SlideShare
Beginning iPhone Development
Beginning iPhone Development
Loading in …3
×
1 of 25
1 of 25

Cocoa Design Patterns

6

Share

Download to read offline

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

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

More Related Content

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Cocoa Design Patterns

  1. 1. Cocoa Design Patterns Stewart Gleadow February 2010
  2. 2. 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
  3. 3. C Objective C
  4. 4. Objective C Syntax Definition: - (void)textDidChangeFrom:(NSString *)old to:(NSString *)newText; { } Calling: [textField textDidChangeFrom:@”old text” to:@”new text”];
  5. 5. ?
  6. 6. Template MVC Enumerators Proxy Method Targets & Flyweight Singleton Delegates Actions Responder Notifications Hierarchies Invocations Chain
  7. 7. Template MVC Enumerators Iterator Proxy Method Targets & Flyweight Singleton Delegates Actions Responder Chain of Notifications Hierarchies Obser ver Composite Responsibility Invocations Command Chain
  8. 8. Template MVC Enumerators Iterator Proxy Method Targets & Flyweight Singleton Delegates Actions Responder Chain of Notifications Hierarchies Obser ver Composite Responsibility Invocations Command Chain
  9. 9. Template Method MyView.h #import <UIKit/UIKit.h> @interface MyView : UIView { // instance variables go here } @end
  10. 10. Template Method MyView.m #import "MyView.h" @implementation MyView - (void)drawRect:(CGRect)rect; { // Drawing code. } @end
  11. 11. Delegate @protocol TTURLRequestDelegate <NSObject> @optional - (void)requestDidStartLoad:(TTURLRequest*)request; - (void)requestDidFinishLoad:(TTURLRequest*)request; @end
  12. 12. Delegate @implementation MyDelegate - (void)requestDidStartLoad:(TTURLRequest*)request; { // start loading indicator? } - (void)requestDidFinishLoad:(TTURLRequest*)request; { // stop loading indicator // do something awesome } @end
  13. 13. Delegate TTURLRequest *request; request = [TTURLRequest requestWithURL:url delegate:myDelegate];
  14. 14. Targets & Actions UIButton *addButton; addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:myButtonReceiver action:@selector(addButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
  15. 15. Enumerator
  16. 16. Proxy
  17. 17. Flyweight
  18. 18. Singleton [[UIApplication sharedApplication] delegate];
  19. 19. Notifications & Invocations
  20. 20. 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)
  21. 21. 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) -&gt; 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&amp;#x2019;s capabilities to C\nThere is a GNU project for the OpenStep standard (but mainly just Apple)\n\n
  • - strict superset of C -&gt; 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&amp;#x201C;don&amp;#x2019;t call us, we&amp;#x2019;ll call you&amp;#x201D;\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 -&gt; easy global storage\n\n
  • - observer/command\n- becomes easier because of KVC/KVO and dynamic message passing\n- debugging? clarity?\n
  • \n
  • \n
  • \n
  • ×