Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Delegation vs. Notification
Search
Jay Thrash
April 26, 2012
Programming
0
190
Delegation vs. Notification
Originally presented at Triangle CocoaHeads on April 26, 2012
Jay Thrash
April 26, 2012
Tweet
Share
More Decks by Jay Thrash
See All by Jay Thrash
Dare to Be Square: Building Adaptive iOS Interfaces
jaythrash
1
200
Good Intentions II: Enemy of the State
jaythrash
1
330
Adventures in Multipeer Connectivity
jaythrash
0
160
Good Intentions: A Path to Better View Controllers
jaythrash
0
510
App Prototyping 101: From Paper to Product
jaythrash
1
270
AltConf 2014: Interaction Prototyping with Origami & Quartz Composer
jaythrash
1
120
Peer Pressure: Adventures in Multipeer Connectivity
jaythrash
0
230
Xcode Alchemy
jaythrash
3
180
Prototyping with Origami
jaythrash
1
860
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
150
Jakarta EE meets AI
ivargrimstad
0
620
Outline View in SwiftUI
1024jp
1
330
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
Amazon Qを使ってIaCを触ろう!
maruto
0
410
Macとオーディオ再生 2024/11/02
yusukeito
0
370
CSC509 Lecture 11
javiergs
PRO
0
180
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Better Code Design in PHP
afilina
PRO
0
130
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Statistics for Hackers
jakevdp
796
220k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Being A Developer After 40
akosma
87
590k
For a Future-Friendly Web
brad_frost
175
9.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Transcript
Delegation vs. Notification Triangle CocoaHeads :: April 2012 Jay Thrash
[email protected]
@jaythrash Saturday, January 18, 14
Goals for This Talk •Understand the difference between Delegation and
Notification •Identify when to use one convention over the other Saturday, January 18, 14
Delegates & Notifications Purpose Saturday, January 18, 14
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
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
Notifications “Spread the word” Saturday, January 18, 14
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
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
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
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
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
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
Notifications Saturday, January 18, 14
Notifications Pros Cons Saturday, January 18, 14
Notifications •One to many •Very lightweight code requirements •Highly Decoupled
•Fire & Forget Pros Cons Saturday, January 18, 14
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
Delegation “Pass the buck” Saturday, January 18, 14
Delegation del∙e∙gate verb |ˈdelәˌgāt| To entrust (a task or responsibility)
to another person Saturday, January 18, 14
Delegation Saturday, January 18, 14
Delegation •Follow the protocol Saturday, January 18, 14
Delegation •Follow the protocol •id Required Saturday, January 18, 14
Delegation •Follow the protocol •id Required •Delegate vs. DataSource Saturday,
January 18, 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; Saturday, January 18, 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; Saturday, January 18, 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; Saturday, January 18, 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; Saturday, January 18, 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; Saturday, January 18, 14
Delegation Saturday, January 18, 14
Delegation Pros Cons Saturday, January 18, 14
Delegation •Minimal coupling •Clearly defined expectations •Easier to debug flow
•Return to sender Pros Cons Saturday, January 18, 14
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
Delegation or Notification? Saturday, January 18, 14
Delegation or Notification? Saturday, January 18, 14
Delegation or Notification? Delegation Notification Saturday, January 18, 14
•One-to-One •Inform & Influence the sender Delegation or Notification? Delegation
Notification Saturday, January 18, 14
•One-to-One •Inform & Influence the sender •Reach Multiple objects •No
Response Necessary Delegation or Notification? Delegation Notification Saturday, January 18, 14
•One-to-One •Inform & Influence the sender •Reach Multiple objects •No
Response Necessary Delegation or Notification? Delegation Notification Saturday, January 18, 14
•One-to-One •Inform & Influence the sender •Reach Multiple objects •No
Response Necessary Delegation or Notification? Delegation Notification Saturday, January 18, 14