iOS Clean Architecture のすすめ

79b6308e9ece8ee9780f8d4a9e0a656d?s=47 koutalou
November 17, 2016

iOS Clean Architecture のすすめ

iOS Clean Architecture の説明と、実際にMoneyForwardでiOS Clean Architectureを導入してみた話をします。

Sample code: https://github.com/koutalou/ios-cleanarchitecture

79b6308e9ece8ee9780f8d4a9e0a656d?s=128

koutalou

November 17, 2016
Tweet

Transcript

  1. iOS Clean Architecture ͷ͢͢Ί 2016/11/17 גࣜձࣾϚωʔϑΥϫʔυ ࣇۄ޹ଠ࿠

  2. ࣗಈՈܭ฽ɾࢿ࢈؅ཧαʔϏεʰϚωʔϑΥϫʔυʱ iOS/AndroidΞϓϦ։ൃΛ୲౰͍ͯ͠·͢ ࣇۄ޹ଠ࿠!LPVUBMPV ΤϯδχΞ ࣗݾ঺հ

  3. औΓ૊Έ 1 $MFBO"SDIJUFDUVSFʹ͍ͭͯ 2 3PVUJOH %FQFOEFODZ*OKFDUJPO 3 $MFBO"SDIJUFDUVSFͷϝϦοτ

  4. औΓ૊Έ 1 $MFBO"SDIJUFDUVSFʹ͍ͭͯ 2 3PVUJOH %FQFOEFODZ*OKFDUJPO 3 $MFBO"SDIJUFDUVSFͷϝϦοτ 2 3

  5. Α͋͘ΔΞʔΩςΫνϟͷ໰୊ w ʮϏδωεϩδοΫ͸.PEFMʹஔ͘΂͖ʯͱݴ͏͕ɺ
 ։ൃऀʹΑͬͯཧղ΍ҙݟ͕ҧ͏ w ౓ॏͳΔ࢓༷มߋ΍ෳࡶͳ࢓༷ʹରԠ͢ΔͨΊʹ7JFX$POUSPMMFS΍
 ಛఆͷ.PEFM͕ංେԽ͠ɺϏδωεϩδοΫͷຊ࣭Λݟࣦ͏ w ಛఆͷϨΠϠ͕ෳ਺ͷ੹຿Λ࣋ͪґଘ౓ͷߴ͍ෳࡶͳίʔυ͕ੜ·ΕΔ ΤϯδχΞ

    ΤϯδχΞ ΤϯδχΞ ͍͢͝࢓༷มߋͩɺɺ ·ͨViewController͕๲ΒΉ ϏδωεϩδοΫ͜Μͳ ॻ͖ํͪ͠Ό͏Μͩɾɾ Model͕͍ΖΜͳॲཧΛ ؚΊͯෳࡶ͗͢Δɾɾ
  6. طଘͷ"SDIJUFDUVSF .7$"SDIJUFDUVSF .71 .77. "SDIJUFDUVSF %%%"SDIJUFDUVSF

  7. $MFBO"SDIJUFDUVSF 7JFX 1SFTFOUFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ 3FQPTJUPSZ 1SFTFOUBUJPO-BZFS

    %PNBJO-BZFS %BUB-BZFS w ੹຿Λࡉ෼Խ͢Δ͜ͱͰɺ֤ϨΠϠ͕ԿΛ୲౰͢Δ͔໌֬ʹͳΔ w 6*ͱϏδωεϩδοΫΛ෼཭Ͱ͖ɺϨΠϠؒͷґଘΛ࠷খݶʹͰ͖Δ w ϨΠϠؒ͸ૄ݁߹ͳͷͰϞδϡʔϧͷஔ͖׵͑ɺςετ͕༰қ
  8. 1SFTFOUBUJPO-BZFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ 3FQPTJUPSZ 1SFTFOUBUJPO-BZFS %PNBJO-BZFS %BUB-BZFS

    w 6*ͷදࣔ΍ΠϕϯτͷϋϯυϦϯάΛߦ͏ w ϏδωεϩδοΫ͸ؔ༩͠ͳ͍ 7JFX 1SFTFOUFS
  9. %PNBJO-BZFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ 1SFTFOUBUJPO-BZFS %PNBJO-BZFS %BUB-BZFS w

    ϏδωεϩδοΫॲཧΛߦ͏ w ϏδωεϩδοΫ
 ɹ3FQPTJUPSZ QBUUFSOΛҙࣝͯ͠ɺऔಘͨ͠σʔλΛ1SFTFOUBUJPO -BZFSʹඞཁͳσʔλʹ੔ܗͯ͠ฦ͢ɺ·ͨ͸ॲཧΛ͢Δ 3FQPTJUPSZ 7JFX 1SFTFOUFS
  10. %BUB-BZFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ 1SFTFOUBUJPO-BZFS %PNBJO-BZFS %BUB-BZFS w

    ௨৴΍σʔλ؅ཧΛߦ͏ w %#΍αʔόͰ؅ཧ͞ΕΔ%BUB4PVSDFΛѻ͏ w 3FQPTJUPSZ͸%PNBJO-BZFS΁ͷ*'Ͱ&OUJUZͷ$36%Λఏڙ͢Δ 3FQPTJUPSZ 7JFX 1SFTFOUFS
  11. 1SFTFOUBUJPO-BZFS 7JFX 1SFTFOUFS 6TF$BTF 1SFTFOUFS w 7JFXͱ6TF$BTFͷΠϕϯτͷڮ౉͠Λߦ͏ w ඞཁʹԠͯ͡7JFXʹ.PEFMΛ౉ͨ͠Γɺ6TF$BTFʹॲཧͤ͞Δ
 7JFX

    7JFX$POUSPMMFS  w 7JFXͷΠϕϯτΛ1SFTFOUFSʹ௨஌͢Δ w ड͚औͬͨ.PEFM΍εςʔλεʹΑΓදࣔΛ੾Γସ͑Δ .PEFM΍4UBUVT৘ใ 7JFXͷ&WFOU .PEFM΍ॲཧ݁Ռ ॲཧͷཁٻ
  12. 1SFTFOUBUJPO-BZFS 7JFX 1SFTFOUFS 6TF$BTF .PEFM΍4UBUVT৘ใ 7JFXͷ&WFOU .PEFM΍ॲཧ݁Ռ ॲཧͷཁٻ

  13. %PNBJO-BZFS 5SBOTMBUPS 1SFTFOUFS 6TF$BTF 6TF$BTF w ϢʔεέʔεʹඞཁͳϩδοΫॲཧΛهड़͢Δ 5SBOTMBUPS w 6TF$BTF͕औಘͨ͠&OUJUZΛ7JFXʹඞཁͳ.PEFMʹม׵͢Δ

    .PEFM w 7JFXͷදࣔʹඞཁͳ7BMVF0CKFDU &OUJUZ .PEFM΍ॲཧ݁Ռ ॲཧͷཁٻ &OUJUZ΍݁Ռ σʔλͷཁٻ 3FQPTJUPSZ .PEFM
  14. %PNBJO-BZFS 5SBOTMBUPS 1SFTFOUFS 6TF$BTF &OUJUZ .PEFM΍ॲཧ݁Ռ ॲཧͷཁٻ &OUJUZ΍݁Ռ σʔλͷཁٻ 3FQPTJUPSZ

    .PEFM
  15. %PNBJO-BZFS 5SBOTMBUPS 1SFTFOUFS 6TF$BTF &OUJUZ .PEFM΍ॲཧ݁Ռ ॲཧͷཁٻ &OUJUZ΍݁Ռ σʔλͷཁٻ 3FQPTJUPSZ

    .PEFM
  16. %BUB-BZFS 6TF$BTF 3FQPTJUPSZ w %BUB4PVSDFͷ$36%૬౰ͷ*' %BUB4UPSF w ࣮ࡍʹ௨৴΍σʔλ؅ཧΛߦ͏ &OUJUZ w

    %#΍αʔόͰ؅ཧ͞ΕΔ7BMVF0CKFDU &OUJUZ΍݁Ռ σʔλͷཁٻ 3FQPTJUPSZ %BUB4UPSF σʔλͷཁٻ &OUJUZ΍݁Ռ /FUXPSL %BUB#BTF σʔλऔಘߋ৽ &OUJUZ΍݁Ռ
  17. %BUB-BZFS 6TF$BTF &OUJUZ΍݁Ռ σʔλͷཁٻ %BUB4UPSF σʔλͷཁٻ &OUJUZ΍݁Ռ /FUXPSL %BUB#BTF σʔλऔಘߋ৽

    &OUJUZ΍݁Ռ 3FQPTJUPSZ
  18. %BUB-BZFS 6TF$BTF &OUJUZ΍݁Ռ σʔλͷཁٻ %BUB4UPSF σʔλͷཁٻ &OUJUZ΍݁Ռ /FUXPSL %BUB#BTF σʔλऔಘߋ৽

    &OUJUZ΍݁Ռ 3FQPTJUPSZ
  19. $MFBO"SDIJUFDUVSF 7JFX 1SFTFOUFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ 3FQPTJUPSZ 1SFTFOUBUJPO-BZFS

    %PNBJO-BZFS %BUB-BZFS w ֤-BZFSͷ੹຿Λ෼͚ɺ࠷খݶͷ*'Ͱૄ݁߹ ˠ֤-BZFS͸ૄ݁߹͚ͩͲɺ͔͜͜ΒͲ͏΍ͬͯಈ͔͢ͷ͔ʁ
  20. औΓ૊Έ $MFBO"SDIJUFDUVSFʹ͍ͭͯ 2 3PVUJOH %FQFOEFODZ*OKFDUJPO $MFBO"S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δϡʔϧͷڞ༗ Ұ෦ͷϞδϡʔϧΛஔ͖׵͑Δ͚ͩͰ৭Μͳը໘ʹରԠՄೳ HomeTimelinePresenterImpl UserTimelinePresenterImpl 7JFX 1SFTFOUFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF

    &OUJUZ 3FQPTJUPSZ 1SFTFOUFSͷΈϞδϡʔϧ͕ҟͳΔྫ
  28. ςετͷ༰қੑ RepositoryMockImpl 7JFX 1SFTFOUFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ 3FQPTJUPSZ

    QSPUPDPMʹ४ڌͨ͠.PDL΍ςετίʔυͰ
 ϨΠϠຖͷॊೈͳςετ͕Մೳ 3FQPTJUPSZΛ.PDLʹͯ͠ &OUJUZˠ.PEFMͷม׵Λςετ 6TF$BTFΛ.PDLʹͯ͠ɺ 7JFXͷ&WFOUʹର͢Δ1SFTFOUFSͷڍಈΛςετ UseCaseMockImpl
  29. 1SPUPDPM0SJFOUFE1SPHSBNNJOH جຊ͸$MFBO"SDIJUFDUVSFΛҙࣝ͢Δ͚ͩͰ101ʹͳΔ w QSPUPDPMΛ࢖ͬͨ*'ʹΑΔ࠷খݶͷґଘؔ܎ w DMBTT͸ۃྗ࢖ΘͣTUSVDUͰ࣮૷͢Δ w QSPUPDPMʹ४ڌ࣮ͨ͠૷͸FYUFOTJPOͰهड़

  30. 3Y4XJGUͱ͍͏બ୒ ϑϨʔϜϫʔΫʹґଘ͠ͳ͍$MFBO"SDIJUFDUVSFͷࢥ૝ʹ
 ൓ͯ͠3Y4XJGUΛ࠾୒͍ͯ͠Δ ɹ1SPT w 6TF$BTF૚Ͱͷ1SPNJTFॲཧͷهड़͕༰қ w 3FQPTJUPSZ૚ҎԼͷ*'ΛετϦʔϜͰهड़ w ը໘ؒ&WFOU௨஌ͷ.VMUJ$BTUΛετϦʔϜͰ࣮૷Մ


    $POT w ࠜװ෦Λ3Y4XJGUͷෆ۩߹΍ϝϯςφϯεঢ়گʹґଘ w 3Y4XJGUࣗମͷֶशίετ
  31. .71ͷ֦ு ݁ہ$MFBO"SDIJUFDUVSFͬͯ.71Λ֦ு͚ͨͩ͠Ͱ͸ʁ ˠ.71Λ֦ு͍ͯ͠Δͱ͍͏఺Ͱ͸ਖ਼͍͠

  32. $MFBO"SDIJUFDUVSFͱ͍͏ఆٛ ݴޠԽ͞Εͨఆ͕ٛ͋Δ͜ͱ͕ॏཁ ఆ͕ٛͳ͍ͱɺ։ൃऀʹΑͬͯཧղ΍ҙݟ͕όϥόϥʹͳͬͯ͠·͏ 7JFX 1SFTFOUFS 6TF$BTF 5SBOTMBUPS .PEFM %BUB4UPSF &OUJUZ

    3FQPTJUPSZ 1SFTFOUBUJPO-BZFS %PNBJO-BZFS %BUB-BZFS "QQMJDBUJPO-BZFS #VJMEFS 8JSFGSBNF
  33. $MFBO"SDIJUFDUVSFͷ1SPT$POT ɹ1SPT w -BZFSຖʹ੹຿͕෼͔ΕϏδωεϩδοΫ͕໌෼Խ w ੹຿͕໌֬ͳͨΊՄಡੑɺϝϯςφϯεੑ͕ߴ͍ w શͯͷ૚ͰςετΛಋೖ͠΍͍͢ w ಛఆͷϑϨʔϜϫʔΫʹґଘ͠ͳ͍

    $POT w ίʔυྔ͕ଟ͍ w $MFBO"SDIJUFDUVSFͷֶशίετ͕ߴ͍
  34. ·ͱΊ

  35. ·ͱΊ %FTJHOFS FS ྑ͍ઃܭΛҙࣝͯ͠ ΑΓྑ͍ίʔυΛॻ͍͍ͯ͜͏ʂ w ֤-BZFSʹ੹຿Λࡉ෼Խ͢Δ͜ͱͰ࣮૷Ͱ໎Θͳ͍ɺՄಡੑ͕ߴ͍ w %*ʹΑΓϞδϡʔϧͷڞ௨Խɺςετͷ༰қੑ͕ߴ͍ w

    ϑϨʔϜϫʔΫʹґଘͤͣߴϝϯςφϯεੑΛ୲อͰ͖Δ $MFBO "SDIJUFDUVSF
  36. 5IBOLZPV