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

VIPER Architecture から学ぶ Dependency Injection

VIPER Architecture から学ぶ Dependency Injection

iOS Test Night #5 - connpass https://testnight.connpass.com/event/59283/

star__hoshi

July 26, 2017
Tweet

More Decks by star__hoshi

Other Decks in Technology

Transcript

  1. VIPER Architecture
    ͔ΒֶͿ
    Dependency Injection
    iOS Test Night #5

    View full-size slide

  2. ࣗݾ঺հ
    • ຊ໊: ੕઒݈հ
    • Github: starhoshi
    • Twitter: @star__hoshi
    • Classi גࣜձࣾ
    • -> 8/1 ͔Β ʁʁʁ

    View full-size slide

  3. VIPER ͷ Router ʹ͍ͭͯ
    IUUQTDIFFTFDBLFMBCTDPNCMPHJPTQSPKFDU
    BSDIJUFDUVSFVTJOHWJQFS

    View full-size slide

  4. ͬ͘͟Γ VIPER
    • View
    • Interactor
    • Presenter
    • Entity
    • Router

    View full-size slide

  5. ͬ͘͟Γ VIPER
    • View - UIViewController
    • Interactor - UseCase(Business Logic)
    • Presenter - View ͱ Interactor Λ஥հ
    • Entity - Data Structure
    • Router - ը໘ભҠͱ Dependency Injection

    View full-size slide

  6. ͬ͘͟Γ VIPER
    • View - UIViewController
    • Interactor - UseCase(Business Logic)
    • Presenter - View ͱ Interactor Λ஥հ
    • Entity - Data Structure
    • Router - ը໘ભҠͱ Dependency Injection

    View full-size slide

  7. Router ͷ໾ׂ

    ը໘ભҠ

    ը໘ભҠͱ Dependency Injection

    View full-size slide

  8. ͜Μͳ͔Μ͡
    IUUQTDIFFTFDBLFMBCTDPNCMPHJPTQSPKFDU
    BSDIJUFDUVSFVTJOHWJQFS

    View full-size slide

  9. ͕࣌ؒͳ͍ͷͰ
    ཧղ͠΍͘͢͢ΔͨΊ
    View, Presenter, Router
    ͚ͩݟ͍͖ͯ·͢

    View full-size slide

  10. View
    protocol UserView: class {
    var presenter: UserPresentation { get }
    init(presenter: UserPresentation)
    }
    final class UserViewController: UIViewController,
    UserView {
    let presenter: UserPresentation
    init(presenter: UserPresentation) {
    self.presenter = presenter
    }
    }

    View full-size slide

  11. Presenter
    protocol UserPresentation: class {
    var router: UserWireframe { get }
    init(router: UserWireframe)
    }
    final class UserPresenter: UserPresentation {
    let router: UserWireframe
    init(router: UserWireframe) {
    self.router = router
    }
    }

    View full-size slide

  12. Router
    protocol UserWireframe: class {
    static func assembleModule() -> UIViewController
    }
    final class UserRouter: UserWireframe {
    static func assembleModule() -> UIViewController {
    let router = UserRouter()
    let presenter = UserPresenter(router: router)
    let view =
    UserViewController(presenter: presenter)
    return view
    }
    }

    View full-size slide

  13. ؔ܎ੑ
    • ந৅ʹґଘ͍ͯ͠Δ
    • ͦΕͧΕ͕ Protocol Λ࣮૷͠ɺ
    Protocol ʹґଘ͍ͯ͠Δ
    • Router ͚͕ͩ۩৅ΫϥεΛ Initialize ͍ͯ͠Δ
    • Router Ͱ DI ͍ͯ͠Δʂ

    View full-size slide

  14. DI ͷୈҰา
    final class UserPresenter: UserPresentation {
    let router: UserWireframe
    init(router: UserWireframe) {
    self.router = router
    }
    }
    final class UserPresenter: UserPresentation {
    let router: UserWireframe
    init() {
    self.router = UserRouter()
    }
    }


    View full-size slide

  15. Կ͕خ͍͠ͷ͔

    View full-size slide

  16. ςετ͕ॻ͖΍͘͢ͳͬͨ
    • Protocol ʹͷΈґଘ͍ͯ͠Δ
    • Mock ΫϥεΛ࡞ΕΔ
    • ґଘؔ܎ͷগͳ͍ Testable ͳΞʔΩςΫνϟʹ
    • ଞͷΞʔΩςΫνϟͰ΋Ԡ༻Ͱ͖Δ

    View full-size slide

  17. ςετίʔυ
    class UserPresenterSpec: QuickSpec {
    override func spec() {
    let router = MockUserRouter()
    let presenter =
    UserPresenter(router: router)
    describe("viewDidLoad") {
    beforeEach {
    presenter.viewDidLoad()
    }
    it("…") { … }
    }
    }
    }

    View full-size slide

  18. ͖ͬ͞ͷίʔυ
    • ͍Ζ͍Ζ୺ંͬͯΔ
    • Presenter ͔Β View ʹ఻͑ΒΕͳ͍ͱ͔
    • Router ͕ը໘ભҠͰ͖ͳ͍ͱ͔
    • ͦ΋ͦ΋ίϯύΠϧ௨Βͳ͍

    View full-size slide

  19. ਅ໘໨ʹ VIPER ͨ͠ίʔυ
    IUUQTHJUIVCDPNTUBSIPTIJ%FWJDF8BMMQBQFS

    View full-size slide

  20. AppStore ʹ΋ग़ͯΔ

    View full-size slide

  21. https://iosdc.jp/2017/node/1543
    Stub ͱ Mock ͷҧ͍ɺͪΌΜ
    ͱཧղͰ͖͍ͯ·͔͢ʁ

    View full-size slide

  22. VIPER Architecture
    ͔ΒֶͿ
    Dependency Injection

    View full-size slide