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
190
Prototyping with Origami
jaythrash
1
860
Other Decks in Programming
See All in Programming
為你自己學 Python
eddie
0
520
AHC041解説
terryu16
0
410
ドメインイベント増えすぎ問題
h0r15h0
2
570
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
0
110
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.4k
rails newと同時に型を書く
aki19035vc
5
710
はてなにおけるfujiwara-wareの活用やecspressoのCI/CD構成 / Fujiwara Tech Conference 2025
cohalz
3
2.8k
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
280
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
450
선언형 UI에서의 상태관리
l2hyunwoo
0
270
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
GraphQLとの向き合い方2022年版
quramy
44
13k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Visualization
eitanlees
146
15k
4 Signs Your Business is Dying
shpigford
182
22k
Bash Introduction
62gerente
610
210k
Music & Morning Musume
bryan
46
6.3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Making the Leap to Tech Lead
cromwellryan
133
9k
Building an army of robots
kneath
302
45k
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