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

Trends & Patterns

Trends & Patterns

Talk given at CocoaHeads Strasbourg.

07cace5d941b67f22d030088283a10f0?s=128

Frédéric Maquin

November 17, 2016
Tweet

Transcript

  1. FRÉDÉRIC MAQUIN @EPHREAD CocoaHeads Strasbourg NOVEMBER 17TH, 2016 PATTERNS Trends

    & ! BETA
  2. There are lots of competing architectural styles and design patterns.

    2
  3. There are lots of competing architectural styles and design patterns.

    (By lots I mean three, really.) 2
  4. MVC CONTROLLER MODEL VIEW 3

  5. MVVM VIEW MODEL MODEL VIEW (UIVIEW + UIVIEWCONTROLLER) Works best

    with Reactive Programming™ 4
  6. VIPER PRESENTER INTERACTOR VIEW (UIVIEW + UIVIEWCONTROLLER) For the worthy

    only. 5 ROUTER ENTITY
  7. — Well-known anonymous person MVC stands for Massive View Controller

    “ ” 6
  8. — Soroush Khanlou MVVM is Not Very Good “ ”

    7
  9. — Ash Furrow MVVM is exceptionally OK “ ” 8

  10. — Random Android guy on the Internet MVP is the

    gold standard “ ” 9
  11. MVVMMVCMVPMVVMCVIPERMVVMMV CMVPMVVMCVIPERMVVMMVCMVPMV VMCVIPEMVVMMVCMVPMVVMCVIPER MVVMMVCMVPMVVMCVIPERRMVVMM VCMVPMVVMCVIPERMVVMMVCMVPM VVMCVIPERMVVMMVCMVPMVVMCVIP ERMVVMMVCMVPMVVMCVIPERMVVM 10

  12. Which one should I choose? 11

  13. Everyone has their own preferences, opinions and biases. 12

  14. — Christian Tietze Don’t force any architectural patterns down your

    throat. Strive for higher testability first. “ ” 13
  15. How do we strive for higher testability and higher maintainability?

    14
  16. SINGLE RESPONSABILITY PRINCIPLE #1 15

  17. Split features across small components. One feature per component. single

    responsability principle 16
  18. single responsability principle class UIViewController: CBCentralManagerDelegate, CLLocationManagerDelegate { var bluetoothManager:

    CBCentralManager! var locationManager: CLLocationManager! func viewDidLoad() { bluetoothManager.delegate = self locationManager.delegate = self } } 17
  19. single responsability principle class UIViewController { var bluetoothManager: BluetoothManager! var

    locationManager: LocationManager! } 17
  20. single responsability principle Andy Matuschak did an amazing talk on

    how to refactor with the SRP in mind. 18
  21. #2 LOOSE COUPLING 19

  22. Loose coupling allows you to test modules independently from each

    other. Use dependency injection to decouple modules. 20
  23. The idea is to provide dependencies, rather than create/retrieve them.

    dependency injection 21
  24. without dependency injection class Model☰ { let electricalMotor: ElectricalMotor =

    InductionMotor() } " Tight Coupling! 22
  25. class Model☰ { let electricalMotor: ElectricalMotor init(electricalMotor: ElectricalMotor) { self.electricalMotor

    = electricalMotor } } class Model☰ViewController { var electricalMotor: ElectricalMotor! } CONSTRUCTOR METHOD with dependency injection 23
  26. class Model☰ { let electricalMotor: ElectricalMotor = Injector.resolve(ElectricalMotor.self) } #

    BAD! ⚠ Inversion of Control is important! with dependency injection 24
  27. tools for dependency injection TYPHOON DIP SWINJECT* [] & *

    my favorite! CLEANSE 25
  28. What about decoupling the app flow? performSegueWithIdentifier("startCar", sender: self) STORYBOARD

    pushViewController(CarStarter(), animated: true) presentViewController(CarStarter(), animated: true, completion: nil) OLD FASHIONED WAY 26
  29. There is a way. 27

  30. New Kid on the Block THE COORDINATOR 28

  31. The coordinator plugs into existing patterns. Except for VIPER, where

    it’s already there. 29
  32. MVVM-C MVC-C
 WHATEVER-C 30

  33. The app coordinator handles the navigation flow, decoupling the controllers

    from each others. coordinator 31
  34. coordinator APP COORDINATOR AUTHENTICATION COORDINATOR FEED COORDINATOR […] AUTHENTICATION VIEW

    CONTROLLER PROFILE VIEW CONTROLLER FEED VIEW CONTROLLER DETAIL VIEW CONTROLLER 32
  35. So, which pattern should you choose? 33

  36. Rule of % Very simple app App with a lot

    of UI states and asynchronous behaviors Very complex app built with a large team MVC MVVM VIPER 34 Not meant to be taken as a hard rule.
  37. Don’t try too hard to pick a fitting pattern. Try

    harder to keep the code clear, testable and maintainable, because… 35
  38. — Christian Tietze You need knowledge and experience and creativity.

    When you can come up with something on your own, you can interpret the rules of VIPER, layered architecture, or whatever else you find on the web properly. You can pick up new ideas and come up with solutions on your own instead of looking for a fitting solution out there. “ ” 36
  39. Questions ? 37

  40. 38 resources iOS Architecture Patterns @borlov MVVM is Not Very

    Good @khanlou MVVM is Exceptionally OK @ashfurrow MVVM Is Quite Okay at What It Is Supposed to Do @ctietze Let’s Play: Refactor the Mega-Controller @andy_matuschak
  41. 39 resources Coordinators Redux @khanlou Improve your iOS Architecture with

    FlowControllers @merowing Coordinators with Storyboards @apokrupto