Suche senden
Hochladen
Using Protocol to Refactor
•
1 gefällt mir
•
406 views
Green Chiu
Folgen
使用 Protocol 來重構 (ObjC) @ CocoaHeads Taipei 2017.11.09
Weniger lesen
Mehr lesen
Ingenieurwesen
Melden
Teilen
Melden
Teilen
1 von 28
Jetzt herunterladen
Downloaden Sie, um offline zu lesen
Empfohlen
Swift core
Swift core
Yusuke Kita
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
Unity Technologies Japan K.K.
Building Windows Images with Packer
Building Windows Images with Packer
Matt Wrock
MacRuby & RubyMotion - Madridrb May 2012
MacRuby & RubyMotion - Madridrb May 2012
Mark Villacampa
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
nsm.nikhil
Introduction to Underscore.js
Introduction to Underscore.js
André Faria Gomes
Cassandra UDF and Materialized Views
Cassandra UDF and Materialized Views
Duyhai Doan
Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
Empfohlen
Swift core
Swift core
Yusuke Kita
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
Unity Technologies Japan K.K.
Building Windows Images with Packer
Building Windows Images with Packer
Matt Wrock
MacRuby & RubyMotion - Madridrb May 2012
MacRuby & RubyMotion - Madridrb May 2012
Mark Villacampa
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
nsm.nikhil
Introduction to Underscore.js
Introduction to Underscore.js
André Faria Gomes
Cassandra UDF and Materialized Views
Cassandra UDF and Materialized Views
Duyhai Doan
Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
es6
es6
Imran shaikh
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
Petr Dvorak
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
Christian Joudrey
iOS 2 - The practical Stuff
iOS 2 - The practical Stuff
Petr Dvorak
Introduction to Underscore.js
Introduction to Underscore.js
David Jacobs
User defined-functions-cassandra-summit-eu-2014
User defined-functions-cassandra-summit-eu-2014
Robert Stupp
How to Write Node.js Module
How to Write Node.js Module
Fred Chien
Node.js Cloud deployment
Node.js Cloud deployment
Nicholas McClay
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
Nilesh Jayanandana
【Unity】Scriptable object 入門と活用例
【Unity】Scriptable object 入門と活用例
Unity Technologies Japan K.K.
ARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマー
Satoshi Asano
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5
Satoshi Asano
Localforage - fast and simple storage library for JavaScript.
Localforage - fast and simple storage library for JavaScript.
Sergey Romaneko
Automating Kubernetes Environments with Ansible
Automating Kubernetes Environments with Ansible
Timothy Appnel
What's New in ES6 for Web Devs
What's New in ES6 for Web Devs
Rami Sayar
Node.js/io.js Native C++ Addons
Node.js/io.js Native C++ Addons
Chris Barber
Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법
Jung Kim
Packer
Packer
Diego Pacheco
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime Web
Bhagaban Behera
Node.js introduction
Node.js introduction
Prasoon Kumar
MFF UK - Introduction to iOS
MFF UK - Introduction to iOS
Petr Dvorak
Cocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design Patterns
Maciej Burda
Weitere ähnliche Inhalte
Was ist angesagt?
es6
es6
Imran shaikh
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
Petr Dvorak
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
Christian Joudrey
iOS 2 - The practical Stuff
iOS 2 - The practical Stuff
Petr Dvorak
Introduction to Underscore.js
Introduction to Underscore.js
David Jacobs
User defined-functions-cassandra-summit-eu-2014
User defined-functions-cassandra-summit-eu-2014
Robert Stupp
How to Write Node.js Module
How to Write Node.js Module
Fred Chien
Node.js Cloud deployment
Node.js Cloud deployment
Nicholas McClay
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
Nilesh Jayanandana
【Unity】Scriptable object 入門と活用例
【Unity】Scriptable object 入門と活用例
Unity Technologies Japan K.K.
ARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマー
Satoshi Asano
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5
Satoshi Asano
Localforage - fast and simple storage library for JavaScript.
Localforage - fast and simple storage library for JavaScript.
Sergey Romaneko
Automating Kubernetes Environments with Ansible
Automating Kubernetes Environments with Ansible
Timothy Appnel
What's New in ES6 for Web Devs
What's New in ES6 for Web Devs
Rami Sayar
Node.js/io.js Native C++ Addons
Node.js/io.js Native C++ Addons
Chris Barber
Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법
Jung Kim
Packer
Packer
Diego Pacheco
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime Web
Bhagaban Behera
Node.js introduction
Node.js introduction
Prasoon Kumar
Was ist angesagt?
(20)
es6
es6
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
iOS 2 - The practical Stuff
iOS 2 - The practical Stuff
Introduction to Underscore.js
Introduction to Underscore.js
User defined-functions-cassandra-summit-eu-2014
User defined-functions-cassandra-summit-eu-2014
How to Write Node.js Module
How to Write Node.js Module
Node.js Cloud deployment
Node.js Cloud deployment
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
【Unity】Scriptable object 入門と活用例
【Unity】Scriptable object 入門と活用例
ARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマー
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5
Localforage - fast and simple storage library for JavaScript.
Localforage - fast and simple storage library for JavaScript.
Automating Kubernetes Environments with Ansible
Automating Kubernetes Environments with Ansible
What's New in ES6 for Web Devs
What's New in ES6 for Web Devs
Node.js/io.js Native C++ Addons
Node.js/io.js Native C++ Addons
Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법
Packer
Packer
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime Web
Node.js introduction
Node.js introduction
Ähnlich wie Using Protocol to Refactor
MFF UK - Introduction to iOS
MFF UK - Introduction to iOS
Petr Dvorak
Cocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design Patterns
Maciej Burda
Building stable testing by isolating network layer
Building stable testing by isolating network layer
Jz Chang
Blocks & GCD
Blocks & GCD
rsebbe
iOS App with Parse.com as RESTful Backend
iOS App with Parse.com as RESTful Backend
Stefano Zanetti
TypeScript for Java Developers
TypeScript for Java Developers
Yakov Fain
Desenvolvimento iOS - Aula 4
Desenvolvimento iOS - Aula 4
Saulo Arruda
Javascript Everywhere
Javascript Everywhere
Pascal Rettig
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
Robert Brown
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
cacois
Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreText
Mugunth Kumar
Bring your code to explore the Azure Data Lake: Execute your .NET/Python/R co...
Bring your code to explore the Azure Data Lake: Execute your .NET/Python/R co...
Michael Rys
iPhone dev intro
iPhone dev intro
Vonbo
Beginning to iPhone development
Beginning to iPhone development
Vonbo
Developing iOS REST Applications
Developing iOS REST Applications
lmrei
Objective-C Is Not Java
Objective-C Is Not Java
Chris Adamson
NestJS
NestJS
Wilson Su
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Igor Bronovskyy
Hujs 总结
Hujs 总结
yiming he
«ReactiveCocoa и MVVM» — Николай Касьянов, SoftWear
«ReactiveCocoa и MVVM» — Николай Касьянов, SoftWear
e-Legion
Ähnlich wie Using Protocol to Refactor
(20)
MFF UK - Introduction to iOS
MFF UK - Introduction to iOS
Cocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design Patterns
Building stable testing by isolating network layer
Building stable testing by isolating network layer
Blocks & GCD
Blocks & GCD
iOS App with Parse.com as RESTful Backend
iOS App with Parse.com as RESTful Backend
TypeScript for Java Developers
TypeScript for Java Developers
Desenvolvimento iOS - Aula 4
Desenvolvimento iOS - Aula 4
Javascript Everywhere
Javascript Everywhere
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreText
Bring your code to explore the Azure Data Lake: Execute your .NET/Python/R co...
Bring your code to explore the Azure Data Lake: Execute your .NET/Python/R co...
iPhone dev intro
iPhone dev intro
Beginning to iPhone development
Beginning to iPhone development
Developing iOS REST Applications
Developing iOS REST Applications
Objective-C Is Not Java
Objective-C Is Not Java
NestJS
NestJS
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Hujs 总结
Hujs 总结
«ReactiveCocoa и MVVM» — Николай Касьянов, SoftWear
«ReactiveCocoa и MVVM» — Николай Касьянов, SoftWear
Kürzlich hochgeladen
KCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitos
Victor Morales
Theory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdf
Shreyas Pandit
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
Drew Moseley
A brief look at visionOS - How to develop app on Apple's Vision Pro
A brief look at visionOS - How to develop app on Apple's Vision Pro
Ray Yuan Liu
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
Researcher Researcher
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Deepika Walanjkar
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
Sneha Padhiar
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
jhunlian
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
mohitesoham12
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
Stephen Sitton
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
elesangwon
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
Romil Mishra
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
Sneha Padhiar
Main Memory Management in Operating System
Main Memory Management in Operating System
Rashmi Bhat
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
bim.edu.pl
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
Manicka Mamallan Andavar
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
prachaibot
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
BalamuruganV28
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
AkritiPradhan2
STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subject
GayathriM270621
Kürzlich hochgeladen
(20)
KCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitos
Theory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdf
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
A brief look at visionOS - How to develop app on Apple's Vision Pro
A brief look at visionOS - How to develop app on Apple's Vision Pro
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
Main Memory Management in Operating System
Main Memory Management in Operating System
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subject
Using Protocol to Refactor
1.
Using Protocol to Refactor 邱志強,
Green Chiu, iOS Developer.
2.
在 iOS ,提到
Protocol 你會想到什什麼?
3.
繼承 ?
4.
Delegation ?
5.
其他 ?
6.
Delegation Pattern • Apple
⼤大量量使⽤用在 CocoaTouch SDK • UITableView, UICollectionView, UIGestureRecognizer and so on. • NSURLSession, StoreKit • 第三⽅方套件
7.
NS-Protocols • 為了了達到特定⽬目的 archive,
copy, enumerate • NSCopying • NSCoding • NSFastEnumeration
8.
繼承 • Objective-C/Swift 不⽀支援多重繼承,但可以實作多個 Protocols •
我們很習慣使⽤用繼承,⼤大部份了了只是為了了部分的 method 或實作 • 可怕的繼承樹 • 在調整後常出現 ”驚喜”
9.
Protocol in Objective-C @protocol
SampleProtocol <NSObject> - (void)sampleMethodA; - (void)sampleMethodB; @optional - (void)sampleOptionalMethod; @end
10.
Protocol 讓⼀一個 class
或 method 知道如何操作物件
11.
The End
12.
重構 • 類似/同樣的程式碼重複出現
13.
Class LocalPlaylistInfo - (void)fetchPlaylistCoverImageWithSize
… { if (…) { … UIImage *image = nil; DBMetaReference *ref = … ; if (ref.sourceType == …) { DBMetaItem *item = …; … } else if (ref.sourceType == …) { MPMediaItem *item = …; image = [item.artwork imageWith …]; } else if (ref.sourceType == LocalDBContextSongSourceTypeStore) { DBMetaItem *item = …; if (!item) { return; } NSString *imageFileURLString = …; void (^imageCallback)(NSString *fileURLString, UIImage *image) = ^(NSString *fileURLString, UIImage *image) { if (image …) { } else if ([NSURL URLWithString:item.photoURL]) { [[KKRadioImageManager sharedImageManager] fetchImageWithURL:… requester:nil callback:^(UIImage *receiveImage, NSError *error) { if (receiveImage) { … } }]; } }; …
14.
Class SongInfoViewModel - (void)loadSongInfo:(LocalSongInfo
*)inSongInfo { if (inSongInfo.type == LocalDBContextSongSourceTypeStore || ...) { DBMetaItem *item = inSongInfo.rawItem; if (inSongInfo.type == LocalDBContextSongSourceTypeStore) { } else { … } self.imageFileURLString = …; void (^imageCallback)(NSString *fileURLString, UIImage *image) = ^void(NSString *fileURLString, UIImage *image){ if (image) { … weakSelf.albumCoverImage = cropImage; } else if ([NSURL URLWithString:item.photoURL]) { [[KKRadioImageManager sharedImageManager] fetchImageWithURL:… requester:nil callback:^(UIImage *receiveImage, NSError *error) { if (receiveImage) { weakSelf.albumCoverImage = … } … }]; } … }; … return; } if (inSongInfo.image) { … } …
15.
Issues • 相似的實作出現在多個地⽅方 • 為了了圖片,View
or Model 載入了了很多 classes/framework
16.
Design Protocol typedef NS_ENUM(NSInteger,
ProvideImageWay) { ProvideImageWayNone = NSNotFound, ProvideImageWayFetchWithURLString = 0, ProvideImageWayGetWithSize, ProvideImageWayGenerateWithCallback }; @protocol LocalItemImageProvider <NSObject> - (ProvideImageWay)getCoverImageWay; - (NSString *)coverURLString; - (UIImage *)coverImageWithSize:(CGSize)inSize; - (void)generateImageWithCallback:(void(^)(UIImage *))inCallback; @end
17.
After implemented - (void)loadSongInfo:(LocalSongInfo
*)inSongInfo { switch ([inSongInfo getCoverImageWay]) { case UPProvideImageWayGetWithSize: self.albumCoverImageView.image = [inSongInfo coverImageWithSize:CGSizeMake(…)]; break; case UPProvideImageWayGenerateWithCallback: { __weak typeof(self) weakSelf = self; [inSongInfo generateImageWithCallback:^(UIImage *image) { weakSelf.albumCoverImageView.image = image; }]; break; } case UPProvideImageWayFetchWithURLString: … break; case UPProvideImageWayNone: … break; } … }
18.
Optimized // UIImageView+LocalItemImageProvider.m - (void)loadImageWithImageProvider:(id<LocalItemImageProvider>)inImageProvider { if
(![inImageProvider conformsToProtocol:@protocol(LocalItemImageProvider)]) { return; } switch ([inImageProvider getCoverImageWay]) { case ProvideImageWayGetWithSize: self.image = [inImageProvider coverImageWithSize:CGSizeMake(44, 44)]; break; case ProvideImageWayGenerateWithCallback: { __weak typeof(self) weakSelf = self; [inImageProvider generateImageWithCallback:^(UIImage *image) { weakSelf.image = image; }]; break; } case ProvideImageWayFetchWithURLString: [self fetchImageWithURLString:[inImageProvider coverURLString]]; break; case ProvideImageWayNone: default: … break; } }
19.
Finally - (void)loadSongInfo:(UPLocalSongInfo *)inSongInfo { [self.albumCoverImageView
loadImageWithImageProvider:inSongInfo]; … }
20.
Besides • 使⽤用 Protocol
讓程式更更容易易被測試 • Mock 物件變得容易易
21.
Testing // UIImageView+LocalItemImageProvider.m - (void)loadImageWithImageProvider:(id<LocalItemImageProvider>)inImageProvider { if
(![inImageProvider conformsToProtocol:@protocol(LocalItemImageProvider)]) { return; } switch ([inImageProvider getCoverImageWay]) { case ProvideImageWayGetWithSize: self.image = [inImageProvider coverImageWithSize:CGSizeMake(44, 44)]; break; case ProvideImageWayGenerateWithCallback: { __weak typeof(self) weakSelf = self; [inImageProvider generateImageWithCallback:^(UIImage *image) { weakSelf.image = image; }]; break; } case ProvideImageWayFetchWithURLString: [self fetchImageWithURLString:[inImageProvider coverURLString]]; break; case ProvideImageWayNone: default: … break; } }
22.
Testing @interface TCDummyLocalImageProvider: NSObject
<LocalItemImageProvider> - (instancetype)initWithType:(ProvideImageWay)inWay; @end @implementation TCDummyLocalImageProvider { ProvideImageWay way; } - (instancetype)initWithType:(ProvideImageWay)inWay { self = [super init]; if (self) { way = inWay; } return self; } - (ProvideImageWay)getCoverImageWay { return way; } ... @end
23.
Testing - (void)testUIImageLoadImageWithImageProvider { UIImageView *imageView
= [[UIImageView alloc] init]; [imageView loadImageWithImageProvider:[NSObject new]]; [imageView loadImageWithImageProvider:[[TCDummyLocalImageProvider alloc] initWithWay:-1000]]; [imageView loadImageWithImageProvider:[[TCDummyLocalImageProvider alloc] initWithWay:ProvideImageWayGetWithSize]]; [imageView loadImageWithImageProvider:[[TCDummyLocalImageProvider alloc] initWithWay:...]]; }
24.
This is Protocol-Oriented Programming
25.
One more thing…
26.
27.
We are hiring iOS
Developer and others
28.
Thanks
Jetzt herunterladen