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

Injection de Dépendance - CocoaHeads Lyon 21/09/17

antdesc
September 21, 2017

Injection de Dépendance - CocoaHeads Lyon 21/09/17

Présentation par Antoine Descamps dans le cadre des CocoaHeads Lyon le 21/09/17 dans les locaux d'Amiltone

antdesc

September 21, 2017
Tweet

More Decks by antdesc

Other Decks in Programming

Transcript

  1. protocol Service { func doSomething() -> Void } extension Service

    { var className: String { return String(describing: type(of: self)) } func doSomething() -> Void { print("Hello "+className) } }
  2. class AlphaService: Service { class var sharedInstance: AlphaService { fileprivate

    struct Singleton { static let instance = AlphaService() } return Singleton.instance } } class MyFirstClass { init() { } func helloWorld() { AlphaService.sharedInstance.doSomething() } } let firstClass = MyFirstClass() firstClass.helloWorld()
  3. class MySecondClass { var service: AlphaService init() { service =

    AlphaService.sharedInstance } func helloWorld() { service.doSomething() } } let secondClass = MySecondClass() secondClass.helloWorld()
  4. class MyThirdClass { var service: AlphaService init(service: AlphaService) { self.service

    = service } func helloWorld() { service.doSomething() } } let thirdClass = MyThirdClass(service: AlphaService.sharedInstance) thirdClass.helloWorld()
  5. class BetaService: Service { init() { } } class MyFifthClass

    { var service: BetaService init(service: BetaService) { self.service = service } func helloWorld() { service.doSomething() } } let betaService = BetaService() let fifthClass = MyFifthClass(service: betaService) fifthClass.helloWorld()
  6. protocol GammaService: Service { } class GammaServiceImpl: GammaService { init()

    { } } class GammaServiceMock: GammaService { init() { } }
  7. class MySixthClass { var service: GammaService init(service: GammaService) { self.service

    = service } func helloWorld() { service.doSomething() } } let gammaService = GammaServiceImpl() let sixthClass = MySixthClass(service: gammaService) sixthClass.helloWorld()
  8. let deltaService = DeltaService() let constructorClass = MyConstructorClass(service: deltaService) constructorClass.helloWorld()

    let parameterClass = MyParameterClass() parameterClass.service = deltaService parameterClass.helloWorld()