Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Delegation vs. Notification by Jay Thrash

Delegation vs. Notification by Jay Thrash

Jay Thrash (@jaythrash) discussed Delegation and Notification in Cocoa at CocoaHeads Raleigh in April 2012

Triangle Cocoa

July 01, 2012
Tweet

More Decks by Triangle Cocoa

Other Decks in Programming

Transcript

  1. Goals for This Talk •Understand the difference between Delegation and

    Noti cation •Identify when to use one convention over the other
  2. Purpose • Apps live in an event- driven world •

    Events are manifested in code as messages • Handle events while remaining architecturally exible • Decouple Communications
  3. Purpose • Apps live in an event- driven world •

    Events are manifested in code as messages • Handle events while remaining architecturally exible • Decouple Communications
  4. Noti cations •Communication via third party - NSNoti cationCenter •Multiple

    objects can react to the same event •Is there anybody out there? Don’t know. Don’t care.
  5. Noti cations •Communication via third party - NSNoti cationCenter •Multiple

    objects can react to the same event •Is there anybody out there? Don’t know. Don’t care.
  6. Noti cations // To send a notification NSString *TCHChocolateAvailable =

    @” TCHChocolateAvailable”; [[NSNotificationCenter defaultCenter] postNotificationName: TCHChocolateAvailable object:self]; // To include a dictionary NSString *TCHProductName = @”TCHProductName”; NSDictionary *msgData = [NSDictionary dictionaryWithObject:@"Snickers" forKey:TCHProductName]; [[NSNotificationCenter defaultCenter] postNotificationName:TCHChocolateAvailable ! ! ! ! ! ! ! ! object:nil ! ! ! ! ! ! ! ! ! ! ! ! userInfo:msgData]; // To receive a notification message [[NSNotificationCenter defaultCenter] addObserver:self ! ! ! selector:@selector(eatChocolate:) ! ! ! name: TCHProductAvailable ! ! ! object:nil]; - (void)eatChocolate:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *productName = [userInfo objectForKey:TCHProductName]; // ... todo eat candy }
  7. Noti cations // To send a notification NSString *TCHChocolateAvailable =

    @” TCHChocolateAvailable”; [[NSNotificationCenter defaultCenter] postNotificationName: TCHChocolateAvailable object:self]; // To include a dictionary NSString *TCHProductName = @”TCHProductName”; NSDictionary *msgData = [NSDictionary dictionaryWithObject:@"Snickers" forKey:TCHProductName]; [[NSNotificationCenter defaultCenter] postNotificationName:TCHChocolateAvailable ! ! ! ! ! ! ! ! object:nil ! ! ! ! ! ! ! ! ! ! ! ! userInfo:msgData]; // To receive a notification message [[NSNotificationCenter defaultCenter] addObserver:self ! ! ! selector:@selector(eatChocolate:) ! ! ! name: TCHProductAvailable ! ! ! object:nil]; - (void)eatChocolate:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *productName = [userInfo objectForKey:TCHProductName]; // ... todo eat candy }
  8. Noti cations // To send a notification NSString *TCHChocolateAvailable =

    @” TCHChocolateAvailable”; [[NSNotificationCenter defaultCenter] postNotificationName: TCHChocolateAvailable object:self]; // To include a dictionary NSString *TCHProductName = @”TCHProductName”; NSDictionary *msgData = [NSDictionary dictionaryWithObject:@"Snickers" forKey:TCHProductName]; [[NSNotificationCenter defaultCenter] postNotificationName:TCHChocolateAvailable ! ! ! ! ! ! ! ! object:nil ! ! ! ! ! ! ! ! ! ! ! ! userInfo:msgData]; // To receive a notification message [[NSNotificationCenter defaultCenter] addObserver:self ! ! ! selector:@selector(eatChocolate:) ! ! ! name: TCHProductAvailable ! ! ! object:nil]; - (void)eatChocolate:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *productName = [userInfo objectForKey:TCHProductName]; // ... todo eat candy }
  9. Noti cations // To send a notification NSString *TCHChocolateAvailable =

    @” TCHChocolateAvailable”; [[NSNotificationCenter defaultCenter] postNotificationName: TCHChocolateAvailable object:self]; // To include a dictionary NSString *TCHProductName = @”TCHProductName”; NSDictionary *msgData = [NSDictionary dictionaryWithObject:@"Snickers" forKey:TCHProductName]; [[NSNotificationCenter defaultCenter] postNotificationName:TCHChocolateAvailable ! ! ! ! ! ! ! ! object:nil ! ! ! ! ! ! ! ! ! ! ! ! userInfo:msgData]; // To receive a notification message [[NSNotificationCenter defaultCenter] addObserver:self ! ! ! selector:@selector(eatChocolate:) ! ! ! name: TCHProductAvailable ! ! ! object:nil]; - (void)eatChocolate:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *productName = [userInfo objectForKey:TCHProductName]; // ... todo eat candy }
  10. Noti cations •One to many •Very lightweight code requirements •Highly

    Decoupled •Fire & Forget •Chaperone Required •No compile-time checks •Manually Managed •Unwanted Feedback •Unknown Delivery Order Pros Cons
  11. Delegation // Protocol defines the delegate’s requirements @protocol TCHMeetupSpeakerDelegate<NSObject> @optional

    // ... - (NSString *) meetUp:(TCHMeetup *)meetUp nameForSpeaker; - (BOOL) meetUp:(TCHMeetup *)meetUp willSpeakForPizza:(NSArray *)pizzaToppings; // ... @end // Another class implements the protocol @interface SpeakerController<TCHMeetupDelegate> @property (nonatomic, retain) TCHMeetup *meetUp; @end // Assign the delegate self.meetUp.delegate = self;
  12. Delegation // Protocol defines the delegate’s requirements @protocol TCHMeetupSpeakerDelegate<NSObject> @optional

    // ... - (NSString *) meetUp:(TCHMeetup *)meetUp nameForSpeaker; - (BOOL) meetUp:(TCHMeetup *)meetUp willSpeakForPizza:(NSArray *)pizzaToppings; // ... @end // Another class implements the protocol @interface SpeakerController<TCHMeetupDelegate> @property (nonatomic, retain) TCHMeetup *meetUp; @end // Assign the delegate self.meetUp.delegate = self;
  13. Delegation // Protocol defines the delegate’s requirements @protocol TCHMeetupSpeakerDelegate<NSObject> @optional

    // ... - (NSString *) meetUp:(TCHMeetup *)meetUp nameForSpeaker; - (BOOL) meetUp:(TCHMeetup *)meetUp willSpeakForPizza:(NSArray *)pizzaToppings; // ... @end // Another class implements the protocol @interface SpeakerController<TCHMeetupDelegate> @property (nonatomic, retain) TCHMeetup *meetUp; @end // Assign the delegate self.meetUp.delegate = self;
  14. Delegation // Protocol defines the delegate’s requirements @protocol TCHMeetupSpeakerDelegate<NSObject> @optional

    // ... - (NSString *) meetUp:(TCHMeetup *)meetUp nameForSpeaker; - (BOOL) meetUp:(TCHMeetup *)meetUp willSpeakForPizza:(NSArray *)pizzaToppings; // ... @end // Another class implements the protocol @interface SpeakerController<TCHMeetupDelegate> @property (nonatomic, retain) TCHMeetup *meetUp; @end // Assign the delegate self.meetUp.delegate = self;
  15. Delegation // Protocol defines the delegate’s requirements @protocol TCHMeetupSpeakerDelegate<NSObject> @optional

    // ... - (NSString *) meetUp:(TCHMeetup *)meetUp nameForSpeaker; - (BOOL) meetUp:(TCHMeetup *)meetUp willSpeakForPizza:(NSArray *)pizzaToppings; // ... @end // Another class implements the protocol @interface SpeakerController<TCHMeetupDelegate> @property (nonatomic, retain) TCHMeetup *meetUp; @end // Assign the delegate self.meetUp.delegate = self;
  16. Delegation •Minimal coupling •Clearly de ned expectations •Easier to debug

    ow •Return to sender •Weak References •Protocols can be code heavy •There can only be one •Bigger commitment Pros Cons
  17. •One-to-One •Inform & In uence the sender •Reach Multiple objects

    •No Response Necessary Delegation or Noti cation? Delegation Noti cation
  18. •One-to-One •Inform & In uence the sender •Reach Multiple objects

    •No Response Necessary Delegation or Noti cation? Delegation Noti cation
  19. •One-to-One •Inform & In uence the sender •Reach Multiple objects

    •No Response Necessary Delegation or Noti cation? Delegation Noti cation