Pro Yearly is on sale from $80 to $50! »

layered arch with swinject

layered arch with swinject

potatotips#34で話した際に使ったスライドです。iOSの話です。

3c4e24fd827c789cb67a9f759f057b06?s=128

Shinnosuke Kugimiya

October 26, 2016
Tweet

Transcript

  1. -BZFSFE"SDIJUFDUVSFͰ ֤૚ͷґଘؔ܎ղܾΛ 4XJOKFDUΛ༻͍ͯߦ͏ QPUBUPUJQT!LHNZTIJO

  2. ࣗݾ঺հ w ఝٶͰ͢ w ΤϯδχΞͰ͢ w ෱Ԭݝ෱Ԭࢢೆ۠໺ଟ໨ग़਎Ͱ͢

  3. ͜ͷൃදΛฉ͘৺ߏ͑ w ͜ͷൃද͸ɺʮ΄Βɺ͜Ε͕ਖ਼ٛͩͥʂʯ͍ͬͯ͏ ΋ͷͰ͸͋Γ·ͤΜɻ w ʮ͜͏͍͏΍Γํ΍ͬͯΈͨΒɺ͖ͬ͢Γͨ͠Αʂ ͳΜ͔ଞʹྑ͍΍Γํ͋ͬͨΒڭ͑ͯ͘Εʯͬͯײ ͡ͷൃදͰ͢ɻ w ͱ͍͏ࢫΛ஌্ͬͨͰฉ͍͍ͯͩ͘͞ɻ

  4. BHFOEB w %*ͱ͸ w -BZFSFE"SDIJUFDUVSFͱ͸ w ࣮૷ྫ w ·ͱΊ

  5. %*ͱ͸

  6. %*ͱ͸ w ґଘੑͷ஫ೖ w ͋ΔΫϥεͷͱ͋ΔৼΔ෣͍ʹ͓͚ΔඞཁͳΦϒ δΣΫτΛ֎෦͔Βηοτͯ͋͛͠Δ͜ͱ

  7. %*͡Όͳ͍࣌OFX class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository() pokemonList = repository.findAll() } }
  8. %*͡Όͳ͍࣌OFX class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository() pokemonList = repository.findAll() } }
  9. %*͡Όͳ͍࣌TJOHMFUPO class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository.sharedInstance pokemonList = repository.findAll() } }
  10. %*͡Όͳ͍࣌TJOHMFUPO class PokemonListViewController: UIViewController { var pokemonList: [Pokemon] = []

    override func viewDidLoad() { let repository = RealmPokemonRepository.sharedInstance pokemonList = repository.findAll() } }
  11. %* class PokemonListViewController: UIViewController { var repository: RealmPokemonRepository? var pokemonList:

    [Pokemon]? override func viewDidLoad() { pokemonList = repository?.findAll() } }
  12. %* class PokemonListViewController: UIViewController { var repository: RealmPokemonRepository? var pokemonList:

    [Pokemon]? override func viewDidLoad() { pokemonList = repository?.findAll() } }
  13. -BZFSFE"SDIJUFDUVSF ͱ͸

  14. -BZFSFE"SDIJUFDUVSFͱ͸ w ҙຯͷ͋Δ·ͱ·ΓΛ૚ͱ͢Δ w ૚ؒͷґଘ͸ૄʹ͢΂͠ w ͦ͏͢Δ͜ͱͰۃ୺ͳ࿩ɺ૚Λؙ͝ͱεύοͱೖΕସ͑ͨ ΓͰ͖Δ w ϓϨθϯςʔγϣϯϨΠϠʔΛεύοͱΞϓϦ͡Όͳͯ͘΋

    ίϚϯυϥΠϯʹ੾Γସ͑ͯ΋࢖͑ΔΑϨϕϧ w ΧʔωϧͩͬͨΓͷԼճΓͰ΋ࣅͨΑ͏ͳߟ͑ํ͋Γ·͢Α Ͷɻ)"-૚ͩͬͨΓɻ
  15. ૚ؒͷґଘ͕ૄͱ͸ w Լͷ૚͸্ͷ૚ʹґଘ͠ͳ͍ ·ͬͨ͘஌Βͳ͍  w ্ͷ૚͸Լͷ૚ͷJOUFSGBDF QSPUPDPM ͔͠஌Βͳ͍ w

    64#Λྫʹ͢ΔͱΘ͔Γ΍͍͔͢΋ʁ
 64#Λ࢖͏ଆ͸ܗͱϓϩτίϧ͚ͩ஌͍ͬͯΕ͹ɺ 64#ଆ͸Կ͕ͬͯ͞͞དྷΔͷ͔શ͘஌Βͳ͍Ͱ 0,ɻ  w ͜͜ʹ%*Λ࢖͏ͱྑ͍
  16. ࣮૷ྫ

  17. ྫ͑͹͜Μͳઃܭ Πϯϑϥ૚ σʔλͷӬଓԽͳͲΛ੹຿ͱ͢Δ υϝΠϯ૚ ۀ຿ϩδοΫ૚ ΞϓϦέʔγϣϯ૚ ΞϓϦͷͨΊͷϩδοΫΛఏڙ͢Δ૚ ϓϨθϯςʔγϣϯ૚ Ͳ͏͍͏෩ʹݟ͑Δ͔Λఏڙ͢Δ૚ ্ͷ૚͸Լͷ૚

    ʹґଘ͍ͯ͠Δ
  18. ֓ཁ w ԼهͷͭΛ࡞ͬͨ w *OGSB$POUBJOFS w %PNBJO$POUBJOFS w "QQMJDBUJPO$POUBJOFS w

    *OKFDUPS
  19. 999$POUBJOFS w 999$POUBJOFS͸࠷্ҐϨΠϠʔҎ֎ͷϨΠϠʔͰ ࡞Δɻ w ͋ΔQSPUPDPMʹରͯ͠Ͳͷ۩ମΫϥεΛฦ͔͢Λ੹ ຿ͱͯ࣋ͭ͠

  20. *OGSB$POUBJOFS w *OGSB-BZFSͰఏڙ͍ͯ͠ΔQSPUPDPMͱ࣮ࡍʹ࢖Θ ΕΔ۩ମΫϥε͕ͲΕͳͷ͔Λఆٛ͢Δ

  21. *OGSB$POUBJOFS class InfraContainer { static func register(container: Container) { container.register(UserBehaviorEventRepository.self)

    { _ in RealmUserBehaviorEventRepository() } container.register(ContentsLoader.self) { _ in InternalContentsLoader() } container.register(AppUpdateChecker.self) { _ in NetworkAppVersionChecker() } container.register(RealmInitializer.self) { r in return RealmInitializer(contentsLoader: r.resolve(ContentsLoader.self)!) } } }
  22. *OGSB$POUBJOFS class InfraContainer { static func register(container: Container) { container.register(UserBehaviorEventRepository.self)

    { _ in RealmUserBehaviorEventRepository() } container.register(ContentsLoader.self) { _ in InternalContentsLoader() } container.register(AppUpdateChecker.self) { _ in NetworkAppVersionChecker() } container.register(RealmInitializer.self) { r in return RealmInitializer(contentsLoader: r.resolve(ContentsLoader.self)!) } } } w ఏڙ͍ͯ͠ΔQSPUPDPM
  23. *OGSB$POUBJOFS class InfraContainer { static func register(container: Container) { container.register(UserBehaviorEventRepository.self)

    { _ in RealmUserBehaviorEventRepository() } container.register(ContentsLoader.self) { _ in InternalContentsLoader() } container.register(AppUpdateChecker.self) { _ in NetworkAppVersionChecker() } container.register(RealmInitializer.self) { r in return RealmInitializer(contentsLoader: r.resolve(ContentsLoader.self)!) } } } w ࣮ࡍʹ࢖ΘΕΔ۩ମΫϥε
  24. ॳظԽ @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var injector: Injector?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let container: Container = Container() InfraContainer.register(container) DomainContainer.register(container) UseCaseContainer.register(container) injector = Injector(container: container) : } : } ֤ϨΠϠʔͷந৅۩ମϚοϓΛొ࿥
  25. ॳظԽ @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var injector: Injector?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let container: Container = Container() InfraContainer.register(container) DomainContainer.register(container) UseCaseContainer.register(container) injector = Injector(container: container) : } : } *OKFDUPSΛ࡞੒
  26. *OKFDUPS w Կ͔ʹґଘ͍ͯ͠ΔΦϒδΣΫτʹґଘ͍ͯ͠Δ΋ͷΛηοτͯ͠ ͋͛Δਓ w *OKFDUPS͸DPOUBJOFSΛϝϯόͱͯ࣋ͭ͠ w 7JFX$POUSPMMFSͳͲґଘͯ͠ΔଆຖʹͦΕΛҾ਺ͱͨ͠JOKFDUϝιο υΛ͍࣋ͬͯΔ w

    *OKFDUPS͸"QQ%FMFHBUF͕࣋ͭ w 7JFX$POUSPMMFSͷWJFX%JE-PBEͳͲͰ BQQ%FMFHBUFJOKFDUPSJOKFDU TFMG Λݺͼग़͢͜ͱͰϝϯόʔ͕JOKFDU ͞ΕΔ
  27. *OKFDUPS class InputViewController: UIViewController, UINavigationControllerDelegate { var userBehaviorEventRepository: UserBehaviorEventRepository? =

    nil : } ͜ͷΑ͏ʹ6TFS#FIBWJPS&WFOU3FQPTJUPSZʹґଘ͢Δ *OQVU7JFX$POUSPMMFS͕͋Δͱ͢Δ
  28. *OKFDUPS class Injector { private let container: Container init(container: Container)

    { self.container = container } func inject(inputViewController: InputViewController) { inputViewController.userBehaviorEventRepository = container.resolve(UserBehaviorEventRepository.self) } func inject(resultViewController: ResultViewController, pokemon: Pokemon) { resultViewController.userBehaviorEventRepository = container.resolve(UserBehaviorEventRepository.self) } } *OKFDUPS͸*OQVU7JFX$POUSPMMFSΛҾ਺ʹ࣋ͬͨJOKFDUϝιο υΛ࣋ͪɺͦ͜Ͱґଘ͍ͯ͠Δ΋ͷΛηοτͯ͋͛͠Δ
  29. *OKFDUPS *OKFDUPSΛ༻͍ͯJOKFDUΛݺͿ class InputViewController: UIViewController, UINavigationControllerDelegate { var userBehaviorEventRepository: UserBehaviorEventRepository?

    = nil override func viewDidLoad() { appDelegate?.injector?.inject(self) : } : }
  30. ͳʹ͕ྑ͍͔ʁ վΊͯ ґଘ͢Δଆ͕શ͘۩ମΫϥεΛ஌Βͳͯ͘ྑ͍ class InputViewController: UIViewController, UINavigationControllerDelegate { var userBehaviorEventRepository:

    UserBehaviorEventRepository? = nil : } த਎͕3FBMNͰͰ͖͍ͯΔͷ͔ͳͲɺ஌Δඞཁͷ ͳ͍͜ͱΛ஌Βͳ͍ɻ
  31. ͳʹ͕ྑ͍͔ʁ σʔλ૚ σʔλͷӬଓԽΛ੹຿ͱ͢Δ υϝΠϯ૚ ۀ຿ϩδοΫ૚ ΞϓϦέʔγϣϯ૚ ΞϓϦͷͨΊͷϩδοΫΛఏڙ͢Δ૚ *OGSB$POUBJOFS %PNBJO$POUBJOFS "QQMJDBUJPO

    $POUBJOFS ͋Δ૚͕ԿΛఏڙ͍ͯ͠Δͷ͔͸ͦͷ૚Ͱͭͷ$POUBJOFS ΛݟΕ͹͙͢Θ͔ΔͷͰɺίʔυΛಡΉͱ͖ʹ໎Θͳ͍ɻ
  32. ·ͱΊ w ֤ϨΠϠʔͰͲͷΫϥεΛఏڙ͢Δͷ͔ʹ͍ͭͯ੹೚Λ࣋ͬͯ Δਓ͕͍Δ w ֤ϨΠϠʔͷґଘͯ͠Δଆ͸ɺଞͷϨΠϠʔͷ۩ମΫϥεΛҰ ੾஌Βͳͯ͘ྑ͍ w 7JFX$POUSPMMFS͕Ͳ͜ʹσʔλΛอଘ͢ΔͳͲͷ͍Βͳ͍৘ ใΛ஌Βͳͯ͘ྑ͍

    w ςετ͠΍͍͢ w γϯάϧτϯʹґଘͨ͠ઃܭ͔Βղ์͞ΕΔ