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
現場で役立つモデリング 超入門
masuda220
PRO
12
2.9k
カスタムしながら理解するGraphQL Connection
yanagii
1
1.2k
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.5k
Sidekiqで実現する 長時間非同期処理の中断と再開 / Pausing and Resuming Long-Running Asynchronous Jobs with Sidekiq
hypermkt
6
2.7k
役立つログに取り組もう
irof
26
8.6k
Vue3の一歩踏み込んだパフォーマンスチューニング2024
hal_spidernight
3
3.1k
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
430
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4k
Golang と Erlang
taiyow
8
1.9k
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
570
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
130
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.1k
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
459
33k
GraphQLの誤解/rethinking-graphql
sonatard
66
9.9k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Side Projects
sachag
452
42k
Designing Experiences People Love
moore
138
23k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
How STYLIGHT went responsive
nonsquared
95
5.2k
Statistics for Hackers
jakevdp
796
220k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
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