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
880
Reduxを取り入れて開発はpairs開発はどう変わったか
satoshin21
0
350
レガシーなアプリケーションの 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
95
Other Decks in Programming
See All in Programming
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
330
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
470
Symfony Mapper Component
soyuka
2
730
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
2
680
선언형 UI에서의 상태관리
l2hyunwoo
0
160
Refactor your code - refactor yourself
xosofox
1
260
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
220
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Rails Girls Zürich Keynote
gr2m
94
13k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Six Lessons from altMBA
skipperchong
27
3.5k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Code Reviewing Like a Champion
maltzj
520
39k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Visualization
eitanlees
146
15k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
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