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
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
140
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
テストコード書いてみませんか?
onopon
2
140
たのしいparse.y
ydah
3
120
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
500
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
840
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
140
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
290
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
230
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
940
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Statistics for Hackers
jakevdp
796
220k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
4 Signs Your Business is Dying
shpigford
181
21k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Scaling GitHub
holman
458
140k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
A Tale of Four Properties
chriscoyier
157
23k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
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