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
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
Zoneless Testing
rainerhahnekamp
0
120
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
180
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
330
ドメインイベント増えすぎ問題
h0r15h0
2
410
快速入門可觀測性
blueswen
0
400
テストコード書いてみませんか?
onopon
2
200
testcontainers のススメ
sgash708
1
130
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
150
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
950
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
440
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
130
Featured
See All Featured
Producing Creativity
orderedlist
PRO
342
39k
Docker and Python
trallard
42
3.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Making Projects Easy
brettharned
116
6k
Done Done
chrislema
182
16k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
How to Ace a Technical Interview
jacobian
276
23k
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