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

iOSアプリの設計とDependency Injection

iOSアプリの設計とDependency Injection

iOSオールスターズ2
https://eventdots.jp/event/602872

Kenichi Yonekawa

November 20, 2016
Tweet

More Decks by Kenichi Yonekawa

Other Decks in Programming

Transcript

  1. Dependency? TUSVDU5XFFUFS\ GVODUXFFU UXFFU4USJOH \ MFUBQJ5XJUUFS"QJ  BQJQPTU5XFFU UXFFU 

    ^ ^ TUSVDU5XJUUFS"QJ\ GVODQPTU5XFFU UXFFU4USJOH \ MFUDPOGJH/463-4FTTJPO$POGJHVSBUJPOEFGBVMU4FTTJPO$POGJHVSBUJPO  MFUTFTTJPO/463-4FTTJPO DPOGJHVSBUJPODPOGJH  MFUSFRVFTUCVJMESFRVFTU TFTTJPOEBUB5BTL8JUI3FRVFTU SFRVFTU  ^ ^ MFUUXFFUFS5XFFUFS  UXFFUFSUXFFU )FMMP  Tweeter TwitterApi HTTP Client
  2. Dependency? TUSVDU5XFFUFS\ GVODUXFFU UXFFU4USJOH \ MFUBQJ5XJUUFS"QJ  BQJQPTU5XFFU UXFFU 

    ^ ^ TUSVDU5XJUUFS"QJ\ GVODQPTU5XFFU UXFFU4USJOH \ MFUDPOGJH/463-4FTTJPO$POGJHVSBUJPOEFGBVMU4FTTJPO$POGJHVSBUJPO  MFUTFTTJPO/463-4FTTJPO DPOGJHVSBUJPODPOGJH  MFUSFRVFTUCVJMESFRVFTU TFTTJPOEBUB5BTL8JUI3FRVFTU SFRVFTU  ^ ^ MFUUXFFUFS5XFFUFS  UXFFUFSUXFFU )FMMP  Tweeter TwitterApi HTTP Client
  3. Solution TUSVDU5XFFUFS\ MFUBQJ5XJUUFS"QJ GVODUXFFU UXFFU4USJOH \ BQJQPTU5XFFU UXFFU  ^

    ^ TUSVDU5XJUUFS"QJ\ MFUIUUQ$MJFOU)551$MJFOU GVODQPTU5XFFU UXFFU4USJOH \ MFUSFRVFTUCVJMESFRVFTU IUUQ$MJFOUTFOE SFRVFTU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UFSTFMG  UXFFUFSUXFFU )FMMP 
  7. Swinject Style MFUDPOUBJOFS$POUBJOFS  DPOUBJOFSSFHJTUFS "OJNBM5ZQFTFMG \@JO$BU OBNF.JNJ ^ DPOUBJOFSSFHJTUFS

    1FSTPO5ZQFTFMG \SJO 1FU0XOFS QFUSSFTPMWF "OJNBM5ZQFTFMG   ^ MFUQFSTPODPOUBJOFSSFTPMWF 1FSTPO5ZQFTFMG  QFSTPOQMBZ 
  8. Cleanse Style TUSVDU"OJNBM.PEVMF$MFBOTF.PEVMF\ GVODDPOGJHVSF##JOEFS CJOEFSCJOEFS# \ CJOEFSCJOE "OJNBM5ZQFTFMG UP WBMVF$BU

    OBNF.JNJ  ^ ^ TUSVDU0XOFS$PNQPOFOU$MFBOTF$PNQPOFOU\ UZQFBMJBT3PPU1FSTPO5ZQF GVODDPOGJHVSF##JOEFS CJOEFSCJOEFS# \ CJOEFSJOTUBMM NPEVMF"OJNBM.PEVMF  CJOEFSCJOE 1FSTPO5ZQFTFMG UP GBDUPSZ1FU0XOFSJOJU  ^ ^ MFUQFSTPOUSZ0XOFS$PNQPOFOU CVJME  QFSTPOQMBZ
  9. Protocol Oriented • ίϯϙʔωϯτؒΛૄ݁߹ʹ͢Δʹ͸ίϯϙʔωϯτͷΠ ϯλϑΣʔεΛ໌ࣔతʹ͠ͳ͍ͱ͍͚ͳ͍ɻΠϯλϑΣʔε ͕ಉ͡ͳΒ࣮૷ͷࠩ͠ସ͕͑༰қʹͳΔɻ QSPUPDPM)551$MJFOU\ GVODTFOE  ^

    DMBTT/463-4FTTJPO$MJFOU)551$MJFOU\ GVODTFOE \ /463-4FTTJPO EBUB5BTL8JUI3FRVFTU ǘ  ^ ^ DMBTT"MBNPGJSF$MJFOU)551$MJFOU\ GVODTFOE \ "MBNPGJSFSFRVFTU ǘ  ^ ^
  10. Constructor Injection vs Property Injection • Property Injection͸֎෦ʹϓϩύςΟΛެ։͢Δඞཁ͕͋ ΓMutableʹ͠ͳ͍ͱ͍͚ͳ͍ͷͰجຊతʹ͸Constructor InjectionΛ͓קΊ͍ͨ͠

    • CleanseͷυΩϡϝϯτͰ͸Property Injection͸ AppDelegate΍StoryboardͳͲΠϯελϯεੜ੒Λ੍ޚͰ ͖ͳ͍έʔεͰ࢖͏ͱ͞Ε͍ͯΔ
  11. prepareForSegue • ભҠݩͷViewController͕ભҠઌͷViewControllerʹ͍ͭͯ ஌Βͳ͍ͱ͍͚ͳ͍ɻભҠઌͷ࢓༷͕มΘͬͨΒભҠݩΛ ௚͞ͳ͍ͱ͍͚ͳ͍ɻ • ΠϯελϯεԽ͸StoryboardʹΑͬͯߦΘΕΔͷͰ PropertyInjectorΛ࢖ͬͯ஫ೖ͢Δ PWFSSJEFGVODQSFQBSF'PS4FHVF TFHVF6*4UPSZCPBSE4FHVF

    TFOEFS "OZ0CKFDU \ JGMFUWDTFHVFEFTUJOBUJPO7JFX$POUSPMMFSBT /FYU7JFX$POUSPMMFS\ MFUJOKFDUPSUSZ/FYU7JFX$POUSPMMFS$PNQPOFOU CVJME  JOKFDUPSJOKFDU1SPQFSUJFT JOUPWD  ^ ^
  12. DI with arguments (Cleanse) TUSVDU/FYU7JFX$PNQPOFOU$MFBOTF$PNQPOFOU\ MFUJUFN*UFN UZQFBMJBT3PPU1SPQFSUZ*OKFDUPS/FYU7JFX$POUSPMMFS GVODDPOGJHVSF##JOEFS CJOEFSCJOEFS# \

    CJOEFSJOTUBMM NPEVMF7JFX.PEFM.PEVMF  CJOEFS CJOE1SPQFSUZ*OKFDUJPO0G /FYU7JFX$POUSPMMFSTFMG  UP\ WD/FYU7JFX$POUSPMMFS WJFX.PEFM7JFX.PEFM JO WJFX.PEFMJUFNJUFN WDWJFX.PEFMWJFX.PEFM ^ ^ ^ PWFSSJEFGVODQSFQBSF'PS4FHVF TFHVF6*4UPSZCPBSE4FHVF TFOEFS"OZ0CKFDU \ JGMFUWDTFHVFEFTUJOBUJPO7JFX$POUSPMMFSBT /FYU7JFX$POUSPMMFS\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  ^FMTF\ JOKFDUPSUSZ$PNQPOFOU CVJME  ^
  15. Do Not Use Fake Mode • ίϯϙʔωϯτఆٛΛ্ॻ͖͢Δͷ͸ίϯϙʔωϯτؒͷґଘͷ ࿈࠯ΛಡΈͮΒ͘͢ΔͷͰΑ͘ͳ͍ʢCleanseͰ΋͜ͷػೳΛ࡟ আ͢ΔPR͕ग़ͯΔʣ •

    ૉ௚ʹϢχοτςετ࣌ʹϞοΫ͢Δ΂͖ɻґଘΦϒδΣΫτ͸ ԿΒ͔ͷܗͰ֎෦͔Β౉ͤΔઃܭʹͳ͍ͬͯΔ͔Β؆୯ͳ͸ͣ DMBTT5XJUUFS"QJ5FTUT9$5FTU$BTF\ WBSTVCKFDU5XJUUFS"QJ MFUTFTTJPO.PDL63-4FTTJPO  PWFSSJEFGVODTFU6Q \ TVQFSTFU6Q  TVCKFDU5XJUUFS"QJ TFTTJPOTFTTJPO  ^ ^