Slide 1

Slide 1 text

Delegation vs. Notification Triangle CocoaHeads :: April 2012 Jay Thrash [email protected] @jaythrash Saturday, January 18, 14

Slide 2

Slide 2 text

Goals for This Talk •Understand the difference between Delegation and Notification •Identify when to use one convention over the other Saturday, January 18, 14

Slide 3

Slide 3 text

Delegates & Notifications Purpose Saturday, January 18, 14

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Notifications “Spread the word” Saturday, January 18, 14

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Notifications Saturday, January 18, 14

Slide 14

Slide 14 text

Notifications Pros Cons Saturday, January 18, 14

Slide 15

Slide 15 text

Notifications •One to many •Very lightweight code requirements •Highly Decoupled •Fire & Forget Pros Cons Saturday, January 18, 14

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Delegation “Pass the buck” Saturday, January 18, 14

Slide 18

Slide 18 text

Delegation del∙e∙gate verb |ˈdelәˌgāt| To entrust (a task or responsibility) to another person Saturday, January 18, 14

Slide 19

Slide 19 text

Delegation Saturday, January 18, 14

Slide 20

Slide 20 text

Delegation •Follow the protocol Saturday, January 18, 14

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Delegation •Follow the protocol •id Required •Delegate vs. DataSource Saturday, January 18, 14

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Delegation Saturday, January 18, 14

Slide 29

Slide 29 text

Delegation Pros Cons Saturday, January 18, 14

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Delegation or Notification? Saturday, January 18, 14

Slide 33

Slide 33 text

Delegation or Notification? Saturday, January 18, 14

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

•One-to-One •Inform & Influence the sender Delegation or Notification? Delegation Notification Saturday, January 18, 14

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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