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
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
250
as(型アサーション)を書く前にできること
marokanatani
4
840
CSC509 Lecture 11
javiergs
PRO
0
180
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
1.7k
Contemporary Test Cases
maaretp
0
120
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
280
EventSourcingの理想と現実
wenas
6
2.3k
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
350
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
650
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
590
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.4k
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.1k
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
RailsConf 2023
tenderlove
29
900
The World Runs on Bad Software
bkeepers
PRO
65
11k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Bash Introduction
62gerente
608
210k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
43
2.2k
Navigating Team Friction
lara
183
14k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Invisible Side of Design
smashingmag
297
50k
Thoughts on Productivity
jonyablonski
67
4.3k
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