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

Delegation vs. Notification

Delegation vs. Notification

Originally presented at Triangle CocoaHeads on April 26, 2012

Jay Thrash

April 26, 2012
Tweet

More Decks by Jay Thrash

Other Decks in Programming

Transcript

  1. Delegation vs. Notification Triangle CocoaHeads :: April 2012 Jay Thrash

    jay.thrash@mac.com @jaythrash Saturday, January 18, 14
  2. Goals for This Talk •Understand the difference between Delegation and

    Notification •Identify when to use one convention over the other Saturday, January 18, 14
  3. Delegates & Notifications Purpose Saturday, January 18, 14

  4. Purpose • Apps live in an event- driven world •

    Events are manifested in code as messages • Handle events while remaining architecturally flexible • Decouple Communications Saturday, January 18, 14
  5. Purpose • Apps live in an event- driven world •

    Events are manifested in code as messages • Handle events while remaining architecturally flexible • Decouple Communications Saturday, January 18, 14
  6. Notifications “Spread the word” Saturday, January 18, 14

  7. Notifications •Communication via third party - NSNotificationCenter •Multiple objects can

    react to the same event •Is there anybody out there? Don’t know. Don’t care. Saturday, January 18, 14
  8. Notifications •Communication via third party - NSNotificationCenter •Multiple objects can

    react to the same event •Is there anybody out there? Don’t know. Don’t care. Saturday, January 18, 14
  9. Notifications // 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 } Saturday, January 18, 14
  10. Notifications // 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 } Saturday, January 18, 14
  11. Notifications // 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 } Saturday, January 18, 14
  12. Notifications // 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 } Saturday, January 18, 14
  13. Notifications Saturday, January 18, 14

  14. Notifications Pros Cons Saturday, January 18, 14

  15. Notifications •One to many •Very lightweight code requirements •Highly Decoupled

    •Fire & Forget Pros Cons Saturday, January 18, 14
  16. Notifications •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 Saturday, January 18, 14
  17. Delegation “Pass the buck” Saturday, January 18, 14

  18. Delegation del∙e∙gate verb |ˈdelәˌgāt| To entrust (a task or responsibility)

    to another person Saturday, January 18, 14
  19. Delegation Saturday, January 18, 14

  20. Delegation •Follow the protocol Saturday, January 18, 14

  21. Delegation •Follow the protocol •id Required Saturday, January 18, 14

  22. Delegation •Follow the protocol •id Required •Delegate vs. DataSource Saturday,

    January 18, 14
  23. 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; Saturday, January 18, 14
  24. 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; Saturday, January 18, 14
  25. 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; Saturday, January 18, 14
  26. 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; Saturday, January 18, 14
  27. 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; Saturday, January 18, 14
  28. Delegation Saturday, January 18, 14

  29. Delegation Pros Cons Saturday, January 18, 14

  30. Delegation •Minimal coupling •Clearly defined expectations •Easier to debug flow

    •Return to sender Pros Cons Saturday, January 18, 14
  31. Delegation •Minimal coupling •Clearly defined expectations •Easier to debug flow

    •Return to sender •Weak References •Protocols can be code heavy •There can only be one •Bigger commitment Pros Cons Saturday, January 18, 14
  32. Delegation or Notification? Saturday, January 18, 14

  33. Delegation or Notification? Saturday, January 18, 14

  34. Delegation or Notification? Delegation Notification Saturday, January 18, 14

  35. •One-to-One •Inform & Influence the sender Delegation or Notification? Delegation

    Notification Saturday, January 18, 14
  36. •One-to-One •Inform & Influence the sender •Reach Multiple objects •No

    Response Necessary Delegation or Notification? Delegation Notification Saturday, January 18, 14
  37. •One-to-One •Inform & Influence the sender •Reach Multiple objects •No

    Response Necessary Delegation or Notification? Delegation Notification Saturday, January 18, 14
  38. •One-to-One •Inform & Influence the sender •Reach Multiple objects •No

    Response Necessary Delegation or Notification? Delegation Notification Saturday, January 18, 14