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

SwiftのDI方法について最近考えてた話 - iOS LT #28

SwiftのDI方法について最近考えてた話 - iOS LT #28

調べた範囲でまとめました

6500a96a5f0786d7ff0c53429d691ae6?s=128

kazuhiro4949

April 26, 2017
Tweet

Transcript

  1. 4XJGUͷ%*ํ๏ʹ͍ͭ ͯ࠷ۙߟ͑ͯͨ࿩ J04-5 ,B[VIJSP)":"4)*

  2. ։ൃʹ%*ίϯςφΛಋೖ͍ͯ͠ΔͷͰ͕͢ɺ ಈతͳੑ࣭͕ਏ͍ͨΊ୤٫Λݕ౼தɻ ͦͷࡍʹௐ΂ͯͨ಺༰ΛγΣΞ͠·͢ɻ

  3. %FQFOEFODZ*OKFDUJPOͱ͸ IUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w l੍ޚͷ൓సzΛߦ͏ͨΊ ͷύλʔϯ w lݺͼग़͠ݩz͕zݺͼग़͠ ઌzʹґଘ͢Δঢ়ଶΛ࣮ ݱ͢Δ w

    ۩ମతͳ࣮૷΁ͷґଘ͕ ֎ʹग़Δ
  4. %FQFOEFODZ*OKFDUJPOͱ͸ ݺͼग़͠ݩͷ.PWJF-JTUFS͕ IUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w l੍ޚͷ൓సzΛߦ͏ͨΊ ͷύλʔϯ w lݺͼग़͠ݩz͕zݺͼग़͠ ઌzʹґଘ͢Δঢ়ଶΛ࣮ ݱ͢Δ

    w ۩ମతͳ࣮૷΁ͷґଘ͕ ֎ʹग़Δ
  5. %FQFOEFODZ*OKFDUJPOͱ͸ ݺͼग़͠ઌͷΠϯλʔϑΣʔ εΛར༻ IUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w l੍ޚͷ൓సzΛߦ͏ͨΊ ͷύλʔϯ w lݺͼग़͠ݩz͕zݺͼग़͠ ઌzʹґଘ͢Δঢ়ଶΛ࣮

    ݱ͢Δ w ۩ମతͳ࣮૷΁ͷґଘ͕ ֎ʹग़Δ
  6. %FQFOEFODZ*OKFDUJPOͱ͸ ݺͼग़͠ઌͷΠϯλʔϑΣʔ εͷத਎ΛΫϥεͱ࣮ͯ͠ ૷ IUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w l੍ޚͷ൓సzΛߦ͏ͨΊ ͷύλʔϯ w lݺͼग़͠ݩz͕zݺͼग़͠

    ઌzʹґଘ͢Δঢ়ଶΛ࣮ ݱ͢Δ w ۩ମతͳ࣮૷΁ͷґଘ͕ ֎ʹग़Δ
  7. %FQFOEFODZ*OKFDUJPOͱ͸ "TTFNCMFSΫϥε͕ .PWJF-JTUFS΁࣮૷Λ஫ೖ ͢Δ IUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w l੍ޚͷ൓సzΛߦ͏ͨΊ ͷύλʔϯ w lݺͼग़͠ݩz͕zݺͼग़͠

    ઌzʹґଘ͢Δঢ়ଶΛ࣮ ݱ͢Δ w ۩ମతͳ࣮૷΁ͷґଘ͕ ֎ʹग़Δ
  8. %FQFOEFODZ*OKFDUJPOͱ͸ w ͜͏ͯ͠ʮ্ҐϨΠϠ͕Լ Ґ΁ґଘ͢Δʯ͕࣮ݱ w Ϣχοτςετ΍ϑϨʔϜ ϫʔΫ੾Γग़͕͠Ͱ͖Δ "QQMJDBUJPO 'SBNFXPSL ݺ͹ΕΔ

    ґଘ ্ҐϨΠϠ ԼҐϨΠϠ
  9. %FQFOEFODZ*OKFDUJPOͱ͸ w ϥΠϒϥϦΛ࢖͏ʮಈతʯͳ΍Γํͱɺͦ͏Ͱ ͸ͳ͍ʮ੩తʯͳ΍Γํ͕͋Δ w ಈతʹ΍Δʹ͸͍ΘΏΔ%*ίϯςφΛಋೖ͢ Δ w ੩తʹߦ͏৔߹͸$BLF1BUUFSOͳͲͷ࣮૷ύ λʔϯ͕͋Δ

  10. %*ίϯςφ

  11. %*ίϯςφ w ґଘؔ܎ͷొ࿥ͱղআΛܾ·ͬͨϧʔϧͰߦ͏ ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔ w ґଘؔ܎͸શͯίϯςφʹΑͬͯొ࿥ɾղܾ͞ ΕΔΑ͏ʹͳΔ

  12. 4XJOKFDU IUUQTHJUIVCDPN4XJOKFDU4XJOKFDU

  13. 4XJOKFDU w0XOFS͸"OJNBMʹґଘ w$BU͸"OJNBMͷ࣮૷ͰOBNF ϓϩύςΟΛ࣋ͭ ྫ͑͹͜Μͳߏ଄ମͱϓϩτίϧ͕͋Δͱͯ͠ IUUQTHJUIVCDPN4XJOKFDU4XJOKFDUͷSFBENFΛࢀߟʹҰ෦վม

  14. 4XJOKFDU wOBNFΛड͚औͬͯ$BUʹ ΠϯδΣΫτ wOBNFΛड͚औͬͯɺͦΕ Λ࢖ͬͯ$BUͷґଘղܾΛ ͯ͠ɺ0XOFS΁౉͢ $POUBJOFSΫϥεͷSFHJTUFSϝιουͰ ґଘؔ܎Λهड़͍ͯ͘͠ IUUQTHJUIVCDPN4XJOKFDU4XJOKFDUͷSFBENFΛࢀߟʹҰ෦վม

  15. 4XJOKFDU IUUQTHJUIVCDPN4XJOKFDU4XJOKFDUͷSFBENFΛࢀߟʹҰ෦վม w0XOFSΛґଘղܾͯ͠ॳ ظԽ $POUBJOFSΫϥεͷSFTPMWFϝιουͰ ґଘؔ܎͕ొ࿥͞Ε͍ͯΔͱ͓Γղܾ͢Δ

  16. 4XJOKFDU  w ొ࿥ɾղܾ͕؆୯͔ܾͭ·ͬͨ΍Γํ ͰͰ͖ͯศར w 4XJGUͰϢχοτςετ͔͚ΔΑ͏ʹͳ Δ

  17. 4XJOKFDUΛ࢖͍ͬͯͯ ੜ·Εͨ՝୊ ґଘղܾͷਖ਼͠͞͸࣮ߦ࣌ʹ͔͠Θ͔ Βͳ͍ ϥΠϒϥϦ΁ϩοΫΠϯ͞Εͯ͠·͏

  18. ґଘղܾͷਖ਼͠͞͸࣮ߦ࣌ʹ ͔͠Θ͔Βͳ͍ ྫ͑͹ઌఔͷΑ͏ʹ0XOFSΛSFTPMWF͢Δ࣌ʹ4USJOHͰ ͸ͳ͘4USJOH Λ౉ͯ͠΋Ϗϧυ͸௨ΓɺΤϥʔ͕ग़ྗ ͞ΕΔ 4USJOHͰ͸ͳ͘0QUJPOBMΛ౉͢

  19. ϥΠϒϥϦ΁ϩοΫΠϯ͞Ε ͯ͠·͏ w ґଘղܾ͕ϥΠϒϥϦ΁ू໿͞ΕɺΞϓϦ શମͰͦΕʹґଘ͢ΔͨΊൈ͚ग़ͤͳ͍ w 4XJGU΍͜ͷϥΠϒϥϦͷόʔδϣϯΞο ϓ΁͍͍ͭͯ͘ͷ͠ΜͲ͍

  20. ίϯύΠϧ࣌νΣοΫ͕Ͱ͖ͯ ΋ͬͱܰྔͳ࢓૊ΈʹҠΓ͍ͨʜ

  21. $BLF1BUUFSOͷ4XJGU࣮૷

  22. $BLF1BUUFSOͷ4XJGU࣮૷ w 1SPUPDPMΛ࢖ͬͯґଘΛίϯϙδγϣϯ͍ͯ͠ ͘ w 1SPUPDPM&YUFOTJPOʹΑͬͯ1SPUPDPMʹఆٛ͞ Εͨม਺ɾϝιουΛ࢖࣮ͬͯ૷Λهड़͢Δ w 4DBMB൛$BLF1BUUFSOͱ͸एׯߏ੒͕ҧ͏

  23. $BLF1BUUFSOͷ4XJGU࣮૷ ίϯύΠϧ࣌νΣοΫ͕Ͱ͖ͯϥΠϒϥϦ΋ෆཁʂ w 1SPUPDPMΛ࢖ͬͯґଘΛίϯϙδγϣϯ͍ͯ͠ ͘ w 1SPUPDPM&YUFOTJPOʹΑͬͯ1SPUPDPMʹఆٛ͞ Εͨม਺ɾϝιουΛ࢖࣮ͬͯ૷Λهड़͢Δ w 4DBMF൛$BLF1BUUFSOͱ͸एׯߏ੒͕ҧ͏

  24. $BLF1BUUFSOͷ4XJGU࣮૷ ͭͷ໾ׂΛ࣋ͭϓϩτίϧ΍ΫϥεΛ࡞Δ ґଘର৅ͷϓϩτίϧͱͦͷ࣮૷ίʔυ ґଘ͢Δͱ͍͏ͷΛఆٛͨ͠ϓϩτίϧ ґଘΛ·ͱΊ্ͨҐϨΠϠͷϓϩτίϧ ґଘղܾʹಛԽͨ͠Ϋϥε

  25. $BLF1BUUFSOͷ4XJGU࣮૷ ͭͷ໾ׂΛ࣋ͭϓϩτίϧ΍ΫϥεΛ࡞Δ ґଘର৅ͷϓϩτίϧͱͦͷ࣮૷ίʔυ ґଘ͢Δͱ͍͏ͷΛఆٛͨ͠ϓϩτίϧ ґଘΛ·ͱΊ্ͨҐϨΠϠͷϓϩτίϧ ґଘղܾʹಛԽͨ͠Ϋϥε Λ্ҐͷϨΠϠͰར༻͠ɺͦ͜Ͱಉ͡΋ͷΛ࡞͍ͬͯ ͖ɺߋʹ্ҐͷϨΠϠͰʜͱ͍͏ͷΛ܁Γฦ͢

  26. ґଘର৅ͷϓϩτίϧͱͦͷ࣮૷ίʔυ

  27. ґଘର৅ͷϓϩτίϧͱͦͷ࣮૷ίʔυ ґଘର৅ͷϓϩτίϧ

  28. ґଘର৅ͷϓϩτίϧͱͦͷ࣮૷ίʔυ ͦͷ࣮૷ίʔυ

  29. ґଘ͢Δͱ͍͏ͷΛఆٛͨ͠ϓϩτίϧ

  30. ґଘ͢Δͱ͍͏ͷΛఆٛͨ͠ϓϩτίϧ ʮͦͷϓϩτίϧΛ ϓϩύςΟͱͯ࣋ͬͯ͠ ͍Δʯͱ͍͏͜ͱΛఆٛ ͨ͠ϓϩτίϧ

  31. ґଘΛ·ͱΊ্ͨҐϨΠϠͷϓϩτίϧ

  32. ґଘΛ·ͱΊ্ͨҐϨΠϠͷϓϩτίϧ Կʹґଘ͢Δ͔Λఆ ্ٛͨ͠ҐϨΠϠͷ ϓϩτίϧ

  33. ґଘΛ·ͱΊ্ͨҐϨΠϠͷϓϩτίϧ ͦͷґଘΛ࢖ͬͨ ॲཧͷ࣮૷

  34. ґଘղܾʹಛԽͨ͠Ϋϥε Ͱ࡞࣮ͬͨ૷ͷ஫ ೖΛهड़ͨ͠Ϋϥε

  35. ґଘղܾʹಛԽͨ͠Ϋϥε ߋʹͦΕʹґଘ͢Δͱ͍ ͏͜ͱΛఆٛͨ͠ ϓϩτίϧΛ༻ҙͯ͠ ্ҐϨΠϠͰ࢖͏ʜ

  36. ৄࡉͳίʔυ IUUQTHPPHM-W/IL

  37. ॴײ w ཁ͢Δʹ1SPUPDPMͷίϯϙδγϣϯͰґଘఆٛɾ ొ࿥Λ͍ͯ͘͠ γϯϓϧ  w $BLF1BUUFSO΋Ͳ͖ͷΈͰ࣮຿ͷ։ൃ΁ແཧͳ ͘ಋೖͰ͖Δ͔ࣗ৴͸࣋ͯͳ͍ w

    ϓϩτίϧ਺͕Ұؾʹ૿͑ͯແବͳෳࡶ͕͞ൃ ੜͦ͠͏
  38. ࣗ෼ͷதͰͷ݁࿦ w ຊ౰ʹඞཁͳͱ͜Ζ͔Β%*Խ͍ͯ͘͠ͱ͍͏ͷ ͕ແ೉͔΋ w ஈ֊తͳ%*Λߦ͏खஈͱͯ͠$BLF1BUUFSO΋Ͳ ͖Λ࢖͍ͬͯ͘ͷ͕ྑͦ͞͏

  39. ࢀߟࢿྉ w 4XJOKFDU w IUUQTHJUIVCDPN4XJOKFDU4XJOKFDU w %FQFOEFODZ*OKFDUJPOJO1SBDUJDF w IUUQTTQFBLFSEFDLDPNZPJDIJUHZEFQFOEFODZJOKFDUJPOJOQSBDUJDFJPTDPO w

    *OWFSTJPOPG$POUSPM$POUBJOFSTBOEUIF%FQFOEFODZ*OKFDUJPOQBUUFSO w IUUQTNBSUJOGPXMFSDPNBSUJDMFTJOKFDUJPOIUNM w .JOJNBM$BLF1BUUFSOJO4XJGU w IUUQTXXXTMJEFTIBSFOFU@ZZV@NJOJNBMDBLFQBUUFSOJOTXJGU w 6TJOHQSPUPDPMDPNQPTJUJPOGPSEFQFOEFODZJOKFDUJPO w IUUQNFSPXJOHJOGPVTJOHQSPUPDPMDPNQPTJUPOGPSEFQFOEFODZJOKFDUJPO
  40. ࣍ճ༧ࠂ w ʮ࣮૷ֶͯ͠ͿϦΞΫςΟϒϓϩάϥϛϯάʯͱ ͍͏λΠτϧͰൃද͍ͨ͠ͱࢥ͍·͢