Cocoa Design Patterns

5,912 views
5,695 views

Published on

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

0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,912
On SlideShare
0
From Embeds
0
Number of Embeds
1,313
Actions
Shares
0
Downloads
74
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • - 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
  • \n
  • \n
  • \n
  • Cocoa Design Patterns

    1. 1. Cocoa Design Patterns Stewart Gleadow February 2010
    2. 2. Timeline 1976 1980 1986 1988 1996 2000 2007 2011 Apple iPhoneOriginal Objective C acquires maniaSmalltalk NeXT NeXT & OS X Mac Commerically NeXTSTEP & app licensed Cocoa store Smalltalk Objective C 2.0
    3. 3. CObjective C
    4. 4. Objective C SyntaxDefinition:- (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 ResponderNotifications Hierarchies Invocations Chain
    7. 7. Template MVC Enumerators Iterator Proxy Method Targets & Flyweight Singleton Delegates Actions Responder Chain ofNotifications Hierarchies Obser ver Composite Responsibility Invocations Command Chain
    8. 8. Template MVC Enumerators Iterator Proxy Method Targets & Flyweight Singleton Delegates Actions Responder Chain ofNotifications Hierarchies Obser ver Composite Responsibility Invocations Command Chain
    9. 9. Template MethodMyView.h#import <UIKit/UIKit.h>@interface MyView : UIView{ // instance variables go here}@end
    10. 10. Template MethodMyView.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. DelegateTTURLRequest *request;request = [TTURLRequest requestWithURL:url delegate:myDelegate];
    14. 14. Targets & ActionsUIButton *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 Thoughtworkssgleadow@thoughtworks.com @stewgleadow

    ×