Upgrade to Pro — share decks privately, control downloads, hide ads and more …

プロトコル指向フレンズ -Class to Struct-

satoshin21
February 15, 2017

プロトコル指向フレンズ -Class to Struct-

オブジェクト指向と対比して取り上げられるプロトコル指向について、ClassとStructそれぞれの特徴をベースにまとめました #potatotips37

satoshin21

February 15, 2017
Tweet

More Decks by satoshin21

Other Decks in Programming

Transcript

  1. ϓϩτίϧࢦ޲ϑϨϯζ
    - Class to Struct -
    @satoshin21

    View full-size slide

  2. satoshin21
    SatoshiN21
    ௕ࡔ ޛࢤ (Nagasaka Satoshi)
    - iOS Engineer of pairs Div. eureka, Inc.
    - Swift, Objective-C, Java(Android), Apple Script

    View full-size slide

  3. - Pairs
    - Couples
    - We are hiring!!

    View full-size slide

  4. Objecive-C͔ΒSwift΁

    View full-size slide

  5. ΦϒδΣΫτࢦ޲(OOP)͔Β
    ϓϩτίϧࢦ޲(POP)΁

    View full-size slide

  6. ϓϩτίϧࢦ޲ͬͯʁ

    View full-size slide

  7. protocolΛ࢖͑͹
    ϓϩτίϧࢦ޲Ͱ͢

    View full-size slide

  8. ͦΜͳ;͏ʹߟ͍͑ͯͨ࣌ظ͕
    ๻ʹ΋͋Γ·ͨ͠

    View full-size slide

  9. POPͱOOP͕ܾఆతʹҧ͏ॴ͸

    View full-size slide

  10. ClassΛ࢖͏͔ʁ
    StructΛ࢖͏͔ʁ

    View full-size slide

  11. ͱߟ͑Δํ͕͍ۙ

    View full-size slide

  12. ΦϒδΣΫτࢦ޲(Class)ͷऑ఺Λ

    View full-size slide

  13. StructͱProtocolͰղܾ͢Δ
    ύϥμΠϜ

    View full-size slide

  14. ͦΕ͕ϓϩτίϧࢦ޲

    View full-size slide

  15. ΦϒδΣΫτࢦ޲ͱ͸ʁ

    View full-size slide

  16. ΦϒδΣΫτࢦ޲
    ‣ &ODBQTVMBUJPO ΧϓηϧԽ

    ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ

    ‣ "CTUSBDUJPO ந৅Խ

    ‣ /BNFTQBDF ໊લۭؒ

    ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋Δߏจɻϝιου΍ϓϩύςΟΛ
    ܨ͛ͯॻ͚ͨΓͳͲ

    ‣ &YUFOTJCJMJUZ ֦ுੑ

    View full-size slide

  17. ΦϒδΣΫτࢦ޲
    ‣ &ODBQTVMBUJPO ΧϓηϧԽ

    ‣ "DDFTT$POUSPM Մࢹੑ੍ޚ

    ‣ "CTUSBDUJPO ந৅Խ

    ‣ /BNFTQBDF ໊લۭؒ

    ‣ &YQSFTTJWF4ZOUBY දݱྗͷ͋Δߏจɻϝιου΍ϓϩύςΟΛ
    ܨ͛ͯॻ͚ͨΓͳͲ

    ‣ &YUFOTJCJMJUZ ֦ுੑ

    ͜ΕΒ͸ܕ(Type)ͷಛ௃

    View full-size slide

  18. ΦϒδΣΫτࢦ޲ͷऑ఺

    View full-size slide

  19. ΦϒδΣΫτࢦ޲ͷऑ఺
    ‣ ҉໧తͳΦϒδΣΫτͷڞ༗
    w ࢀর͍ͯ͠ΔΦϒδΣΫτ͕ଞͷΦϒδΣΫτͱڞ༗
    w ʮࢀর͍ͯ͠ΔΦϒδΣΫτ͕উखʹҧ͏΋ͷʹͳͬͨΑʂͳʹ
    ͜Εͳʹ͜Εʂʯ

    View full-size slide

  20. ΦϒδΣΫτࢦ޲ͷऑ఺
    ‣ ܧঝؔ܎Λ೺Ѳ͢Δͷ͕େม
    w ͜ͷ6TFS%FUBJM7JFX$POUSPMMFS͸%FUBJM7JFX$POUSPMMFSΛܧঝ
    ͍ͯͯ͠͞ΒʹͦͷεʔύʔΫϥεʹ͸#BTF7JFX$POUSPMMFS͕
    w εʔύʔΫϥε͕ͲͷΑ͏ͳڍಈΛ͢Δͷ͔ɺͲͷΑ͏ͳΦϒδΣ
    ΫτΛอ͍࣋ͯ͠Δͷ͔Λ೺Ѳ͠ͳ͚Ε͹ͳΒͳ͍
    w ʮ͜ͷεʔύʔΫϥεͷ஋Λमਖ਼ͨ͠Βಈ͔ͳ͘ͳͬͨΑʔʂ



    ͢͝ʔ͍ʂʯ

    View full-size slide

  21. ͦ͜Ͱొ৔͢Δͷ͕
    ϓϩτίϧࢦ޲

    View full-size slide

  22. ϓϩτίϧࢦ޲Ͱ͸جຊతʹ
    Class͸࢖Θͳ͍

    View full-size slide

  23. StructͱProtocolͷΈ

    View full-size slide

  24. Class to Struct

    View full-size slide

  25. ClassͱStruct
    $MBTT 4USVDU
    ܕ 5ZQF
    ࢀরܕ ஋ܕ
    ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ

    View full-size slide

  26. class DataStore {
    var strings = [String]()
    func add(string: String) {
    strings.append(string)
    }
    }
    ClassΛ༻͍ͨ৔߹

    View full-size slide

  27. 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Λ༻͍ͨ৔߹

    View full-size slide

  28. 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Λ༻͍ͨ৔߹

    View full-size slide

  29. Structͷ৔߹

    View full-size slide

  30. struct DataStore {
    var strings = [String]()
    mutating func add(string: String) {
    strings.append(string)
    }
    }
    StructΛ༻͍ͨ৔߹

    View full-size slide

  31. 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Λ༻͍ͨ৔߹

    View full-size slide

  32. 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Λ༻͍ͨ৔߹

    View full-size slide

  33. ࢀরઌ͕มԽ͢Δ͜ͱΛ
    ؾʹ͠ͳͯ͘ྑ͍

    View full-size slide

  34. ͨͩɺStruct͸ܧঝ͕࢖͑ͳ͍

    View full-size slide

  35. is-aؔ܎ͱ͔ಉ͡;Δ·͍ఆٛ
    ͢Δͱ͖ͱ͔Ͳ͏͢Μͷʁ

    View full-size slide

  36. ͦ͜ͰProtocol

    View full-size slide

  37. ΦϒδΣΫτࢦ޲ʹΑΔܧঝؔ܎
    "OZ0CKFDU
    &NQMPZFF
    %FWFMPQFS
    %JSFDUPS
    J04%FWFMPQFS ʜ

    View full-size slide

  38. ϓϩτίϧࢦ޲
    )BT1SPHSBNNJOH4LJMM
    %JSFDUPS J04%FWFMPQFS "OESPJE%FWFMPQFS
    )BT.BOBHFNFOU4LJMM

    View full-size slide

  39. ϓϩτίϧࢦ޲ͷར఺
    ‣ ґଘؔ܎ΛϑϥοτʹͰ͖Δ
    ‣ ؔ܎ੑ΋௥͍΍͍͢
    ‣ 1SPUPDPM&YUFOTJPOʹΑΓɺΦϒδΣΫτࢦ޲ͷܧঝͰ࣮ݱ͖ͯ͠
    ͨίʔυͷ࠶ར༻ੑ΋อͯΔ

    View full-size slide

  40. Swift Standart LibraryͰ͸ɺ
    طʹclass͸1ͭΛ࢒͢ͷΈ

    View full-size slide

  41. ͜ΕͰ໌೔͔Β
    ϓϩτίϧࢦ޲ϑϨϯζͩͶʔʂ

    View full-size slide

  42. ͙͢ʹ͸೉ͦ͠͏

    View full-size slide

  43. ϓϩτίϧࢦ޲ͷ໰୊఺
    ‣ ΦʔϓϯιʔεԽ͞Εͯ͸͍Δ͕ɺະͩ4XJGU͸J04 ͳ͍͠͸
    NBD04ΞϓϦ։ൃݴޠͷΠϝʔδ͕ڧ͍
    ‣ ͭ·Γɺ$PDPB'SBNFXPSLͱ஥ྑ͘͠ͳ͚Ε͹ͳΒͳ͍
    ‣ $PDPB'SBNFXPSL͸DMBTTϕʔε
    ‣ ΦϒδΣΫτࢦ޲ͱ൱͕ԠͰ΋෇͖߹͏ඞཁ͕͋Δ
    ‣ ͦΕ͸ɺΦϒδΣΫτࢦ޲ͷةݥੑ ҉໧తΦϒδΣΫτڞ༗
    Λ͸
    ΒΜͰ͍Δͱ͍͏͜ͱ

    View full-size slide

  44. ·ͨɺແཧʹ
    classͱstructΛڞଘͤ͞Δͱɾɾ

    View full-size slide

  45. ClassͱStruct
    $MBTT 4USVDU
    ܕ 5ZQF
    ࢀরܕ ஋ܕ
    ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ

    View full-size slide

  46. ClassͱStruct
    $MBTT 4USVDU
    ܕ 5ZQF
    ࢀরܕ ஋ܕ
    ܧঝ Ͱ͖Δ Ͱ͖ͳ͍
    ϓϩύςΟมߋ Մೳ جຊతʹෆՄೳ
    ͜ͷҧ͍Λৗʹҙࣝ͢Δඞཁ͕͋Δ

    View full-size slide

  47. ·ͩզʑ͸
    ΦϒδΣΫτࢦ޲͔Β཭ΕΒΕͳ͍

    View full-size slide

  48. Swift͸ϚϧνύϥμΠϜͳͷͰ
    ͏·͘ڞଘ͍ͤͯ͘͞ࣄ͕ॏཁ

    View full-size slide

  49. ΁ʔ͖΁ʔ͖ʂ

    View full-size slide

  50. ύϥμΠϜʹΑͬͯ
    ͱ͍͘ͳ͜ͱҧ͏͔Βʂ

    View full-size slide

  51. ֤ύϥμΠϜͷಛੑΛཧղͯ͠
    ॊೈͳϓϩάϥϛϯάΛ

    View full-size slide

  52. Thank you
    IUUQTXXXOBTBHPWNJTTJPO@QBHFT/11OFXTFBSUIBUOJHIUIUNM

    View full-size slide