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

WWDC2016で話されたDependency Injectionを活用してリファクタリング

WWDC2016で話されたDependency Injectionを活用してリファクタリング

Presentation at potato tips #32 on August 23, 2016.
http://potatotips.connpass.com/event/36346/

Yoichi Tagaya

August 23, 2016
Tweet

More Decks by Yoichi Tagaya

Other Decks in Programming

Transcript

  1. SwinjectΛ஌͍ͬͯΔͱ༗རͳब৬ઌ • Atlassian • Implement unit tests and performance tests

    in Objective-C and Swift using XCTest, OCMock, Swinject... • https://www.smartrecruiters.com/Atlassian/93336663-ios-developer • MealPass • Familiarity with dependency injection libraries such as swinject • http://garysguide.com/jobs/up0gwed/Software-Engineer-at-MealPass-New-York-NY • Location Labs • We are using in our iOS products, including Cocoapods, Typhoon/ Swinject, Realm, PromiseKit, Alamofire, Amplitude...
 https://www.latitude.work/locationlabs/emeryville/senior-software-engineer-ios • МАДЖЕСТИ • 1. Realm, 2. Dependency injection (Swinject), 3. Работа с платежными... • https://voronezh.hh.ru/vacancy/17552731
  2. WWDC 2016: "Improving Existing Apps with Modern Best Practices" by

    Woody View Controller͸ྫ͑ͯݴ͏ͳΒ LEGOϒϩοΫͳͷ͞ɻ ಠཱͨ͠΋ͷΛ૊ΈཱͯͯશମΛ ߏ੒͢ΔΜͩʂ ؆୯ʹ૊Έཱ͍ͯͨͳΒ Dependency Injection Λ ࢖͏ͱϝνϟศརͩͥʂ https://developer.apple.com/videos/play/wwdc2016/213/ ɹat 21:45
  3. ѱ͍ྫ: AppDelegateʹґଘ class MyTableViewController: UITableViewController { override func tableView(tableView: UITableView,

    numberOfRowsInSection section: Int) -> Int { return (UIApplication.sharedApplication().delegate as? AppDelegate)?.content?.count ?? 0 } } AppDelegateʹґଘ (͔͠΋γϯάϧτϯ)
  4. ґଘΛProtocolʹ੾Γग़ͯ͠ ϦϑΝΫλϦϯά protocol ContentProvider { var content: Content { get

    } } class AppDelegate: UIResponder, UIApplicationDelegate, ContentProvider { private (set) var content = Content() } class MyTableViewController: UITableViewController { var contentProvider: ContentProvider? override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return contentProvider?.content.count ?? 0 } } ඞཁͳ෦෼͚ͩProtocolʹ੾Γग़͢ AppDelegateΛProtocolʹద߹ͤ͞Δ Protocolʹ͚ͩґଘ͠ɺ࣮ମͷΠϯελϯε͸֎͔Β౉ͯ͠΋Β͏
  5. ଞʹ΋͍Ζ͍Ζґଘ͕͋Δ৔߹ protocol ViewManager { func presentViewControllerA() func presentViewControllerB() } extension

    AppDelegate: ViewManager { func presentViewControllerA() { ... } func presentViewControllerB() { ... } } class MyTableViewController: UITableViewController { var app: protocol<ContentProvider, ViewManager>? override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return app?.content.count ?? 0 } func doSomething() { app?.presentViewControllerA() } } ଞͷ෦෼΋άϧʔϓ͝ͱʹProtocolʹ੾Γग़͢ AppDelegateΛͦͷProtocolʹద߹ͤ͞Δ (extension͕ਪ঑) ෳ਺ͷProtocolΛ·ͱΊͨΠϯελϯεΛͱΔΑ͏ʹ͢Δ
  6. ຤୺ͷView Controller͸ඞཁͳ ProtocolͷΈͷܕͰड͚औΔ "QQ%FMFHBUF தؒ 7JFX$POUSPMMFS ຤୺ 7JFX$POUSPMMFS protocol<ContentProvider, ViewManager>ܕ

    ͷϓϩύςΟΛఆٛ ContentProviderܕͷϓϩύςΟΛఆٛ (౉࣮͢ଶ͸AppDelegateͰ͍͍) (౉࣮͢ଶ͸AppDelegateͰ͍͍)