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
プロトコル指向フレンズ -Class to Struct-
Search
satoshin21
February 15, 2017
Programming
3
1.8k
プロトコル指向フレンズ -Class to Struct-
オブジェクト指向と対比して取り上げられるプロトコル指向について、ClassとStructそれぞれの特徴をベースにまとめました #potatotips37
satoshin21
February 15, 2017
Tweet
Share
More Decks by satoshin21
See All by satoshin21
少数精鋭で戦うための技術的改善について
satoshin21
3
1.1k
GTXiLibで小さく始めるAccessibility Testing
satoshin21
0
4.9k
iPhoneのカメラで写真撮影から現像までの技術を紐解く
satoshin21
4
3.2k
try! swift-sh
satoshin21
2
850
Reduxを取り入れて開発はpairs開発はどう変わったか
satoshin21
0
340
レガシーなアプリケーションの 60fps化を目指す為にやっていること
satoshin21
12
3.8k
Introducing CodeLayout with Tips
satoshin21
6
1.5k
World of No Interface Builder
satoshin21
0
1.8k
What I've done to attend WWDC
satoshin21
0
90
Other Decks in Programming
See All in Programming
Better Code Design in PHP
afilina
PRO
0
120
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.2k
Identifying User Idenity
moro
6
9.7k
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
210
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
520
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
4
1.1k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
220
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
280
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
350
CSC509 Lecture 09
javiergs
PRO
0
140
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
136
6.6k
Six Lessons from altMBA
skipperchong
27
3.5k
Happy Clients
brianwarren
98
6.7k
Adopting Sorbet at Scale
ufuk
73
9.1k
Documentation Writing (for coders)
carmenintech
65
4.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Thoughts on Productivity
jonyablonski
67
4.3k
Building Adaptive Systems
keathley
38
2.3k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Ruby is Unlike a Banana
tanoku
96
11k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Transcript
ϓϩτίϧࢦϑϨϯζ - Class to Struct - @satoshin21
satoshin21 SatoshiN21 ࡔ ޛࢤ (Nagasaka Satoshi) - iOS Engineer of
pairs Div. eureka, Inc. - Swift, Objective-C, Java(Android), Apple Script
- Pairs - Couples - We are hiring!!
Objecive-C͔ΒSwift
ΦϒδΣΫτࢦ(OOP)͔Β ϓϩτίϧࢦ(POP)
ϓϩτίϧࢦͬͯʁ
protocolΛ͑ ϓϩτίϧࢦͰ͢
ͦΜͳ;͏ʹߟ͍͑ͯͨ࣌ظ͕ ʹ͋Γ·ͨ͠
POPͱOOP͕ܾఆతʹҧ͏ॴ
ClassΛ͏͔ʁ StructΛ͏͔ʁ
ͱߟ͑Δํ͕͍ۙ
͘͠
ΦϒδΣΫτࢦ(Class)ͷऑΛ
StructͱProtocolͰղܾ͢Δ ύϥμΠϜ
ͦΕ͕ϓϩτίϧࢦ
ΦϒδΣΫτࢦͱʁ
ΦϒδΣΫτࢦ ‣ &ODBQTVMBUJPO ΧϓηϧԽ ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ ‣
"CTUSBDUJPO நԽ ‣ /BNFTQBDF ໊લۭؒ ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋ΔߏจɻϝιουϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ ‣ &YUFOTJCJMJUZ ֦ுੑ
ΦϒδΣΫτࢦ ‣ &ODBQTVMBUJPO ΧϓηϧԽ ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ ‣
"CTUSBDUJPO நԽ ‣ /BNFTQBDF ໊લۭؒ ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋ΔߏจɻϝιουϓϩύςΟΛ ܨ͛ͯॻ͚ͨΓͳͲ ‣ &YUFOTJCJMJUZ ֦ுੑ ͜ΕΒܕ(Type)ͷಛ
ΦϒδΣΫτࢦͷऑ
ΦϒδΣΫτࢦͷऑ ‣ ҉తͳΦϒδΣΫτͷڞ༗ w ࢀর͍ͯ͠ΔΦϒδΣΫτ͕ଞͷΦϒδΣΫτͱڞ༗ w ʮࢀর͍ͯ͠ΔΦϒδΣΫτ͕উखʹҧ͏ͷʹͳͬͨΑʂͳʹ ͜Εͳʹ͜Εʂʯ
ΦϒδΣΫτࢦͷऑ ‣ ܧঝؔΛѲ͢Δͷ͕େม w ͜ͷ6TFS%FUBJM7JFX$POUSPMMFS%FUBJM7JFX$POUSPMMFSΛܧঝ ͍ͯͯ͠͞ΒʹͦͷεʔύʔΫϥεʹ#BTF7JFX$POUSPMMFS͕ w εʔύʔΫϥε͕ͲͷΑ͏ͳڍಈΛ͢Δͷ͔ɺͲͷΑ͏ͳΦϒδΣ ΫτΛอ͍࣋ͯ͠Δͷ͔ΛѲ͠ͳ͚ΕͳΒͳ͍ w
ʮ͜ͷεʔύʔΫϥεͷΛमਖ਼ͨ͠Βಈ͔ͳ͘ͳͬͨΑʔʂ ͢͝ʔ͍ʂʯ
ͳͲ
ͦ͜Ͱొ͢Δͷ͕ ϓϩτίϧࢦ
ϓϩτίϧࢦͰجຊతʹ ClassΘͳ͍
StructͱProtocolͷΈ
Class to Struct
ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
class DataStore { var strings = [String]() func add(string: String)
{ strings.append(string) } } ClassΛ༻͍ͨ߹
let dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: "Bar") let classA
= ReferenceClassA(dataStore: dataStore) classA.add(string: "hoge") let stringA = classA.dataStoreString() // -> [“Foo","Bar","hoge"] ClassΛ༻͍ͨ߹
let dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: “Bar") let classA
= ReferenceClassA(dataStore: dataStore) classA.add(string: "hoge") let stringA = classA.dataStoreString() // -> ["Foo","Bar","hoge"] let classB = ReferenceClassB(dataStore: dataStore) classB.dataStore.strings.removeAll() let stringAre = classA.dataStoreString() // -> [] ClassΛ༻͍ͨ߹
Structͷ߹
struct DataStore { var strings = [String]() mutating func add(string:
String) { strings.append(string) } } StructΛ༻͍ͨ߹
var dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: "Bar") var structA
= ReferenceStructA(dataStore: dataStore) structA.dataStore.add(string: "hoge") let stringA = structA.dataStoreString() // -> [“Foo","Bar","hoge"] StructΛ༻͍ͨ߹
var dataStore = DataStore() dataStore.add(string: "Foo") dataStore.add(string: "Bar") var structA
= ReferenceStructA(dataStore: dataStore) structA.dataStore.add(string: "hoge") let stringA = structA.dataStoreString() // -> ["Foo","Bar","hoge"] var structB = ReferenceStructB(dataStore: dataStore) structB.dataStore.strings.removeAll() let stringAre = structA.dataStoreString() // -> ["Foo","Bar","hoge"] StructΛ༻͍ͨ߹
ࢀরઌ͕มԽ͢Δ͜ͱΛ ؾʹ͠ͳͯ͘ྑ͍
ͨͩɺStructܧঝ͕͑ͳ͍
is-aؔͱ͔ಉ͡;Δ·͍ఆٛ ͢Δͱ͖ͱ͔Ͳ͏͢Μͷʁ
ͦ͜ͰProtocol
ΦϒδΣΫτࢦʹΑΔܧঝؔ "OZ0CKFDU &NQMPZFF %FWFMPQFS %JSFDUPS J04%FWFMPQFS ʜ
ϓϩτίϧࢦ )BT1SPHSBNNJOH4LJMM %JSFDUPS J04%FWFMPQFS "OESPJE%FWFMPQFS )BT.BOBHFNFOU4LJMM
ϓϩτίϧࢦͷར ‣ ґଘؔΛϑϥοτʹͰ͖Δ ‣ ؔੑ͍͍͢ ‣ 1SPUPDPM&YUFOTJPOʹΑΓɺΦϒδΣΫτࢦͷܧঝͰ࣮ݱ͖ͯ͠ ͨίʔυͷ࠶ར༻ੑอͯΔ
Swift Standart LibraryͰɺ طʹclass1ͭΛ͢ͷΈ
͜ΕͰ໌͔Β ϓϩτίϧࢦϑϨϯζͩͶʔʂ
͙͢ʹͦ͠͏
ϓϩτίϧࢦͷ ‣ ΦʔϓϯιʔεԽ͞Ε͍ͯΔ͕ɺະͩ4XJGUJ04 ͳ͍͠ NBD04ΞϓϦ։ൃݴޠͷΠϝʔδ͕ڧ͍ ‣ ͭ·Γɺ$PDPB'SBNFXPSLͱྑ͘͠ͳ͚ΕͳΒͳ͍ ‣ $PDPB'SBNFXPSLDMBTTϕʔε ‣
ΦϒδΣΫτࢦͱ൱͕ԠͰ͖߹͏ඞཁ͕͋Δ ‣ ͦΕɺΦϒδΣΫτࢦͷةݥੑ ҉తΦϒδΣΫτڞ༗ Λ ΒΜͰ͍Δͱ͍͏͜ͱ
·ͨɺແཧʹ classͱstructΛڞଘͤ͞Δͱɾɾ
ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
ClassͱStruct $MBTT 4USVDU ܕ 5ZQF ࢀরܕ ܕ ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ ͜ͷҧ͍Λৗʹҙࣝ͢Δඞཁ͕͋Δ
·ͩզʑ ΦϒδΣΫτࢦ͔ΒΕΒΕͳ͍
SwiftϚϧνύϥμΠϜͳͷͰ ͏·͘ڞଘ͍ͤͯ͘͞ࣄ͕ॏཁ
ʔ͖ʔ͖ʂ
ύϥμΠϜʹΑͬͯ ͱ͍͘ͳ͜ͱҧ͏͔Βʂ
֤ύϥμΠϜͷಛੑΛཧղͯ͠ ॊೈͳϓϩάϥϛϯάΛ
Thank you IUUQTXXXOBTBHPWNJTTJPO@QBHFT/11OFXTFBSUIBUOJHIUIUNM