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

Clean Architecture in Swift apps.

Ivan Rublev
December 10, 2016

Clean Architecture in Swift apps.

My keynote from IT61 Mobile Meetup. I told about the Clean Architecture by Robert C. Martin (Uncle Bob) and it's application to making Swift apps.

Ivan Rublev

December 10, 2016
Tweet

More Decks by Ivan Rublev

Other Decks in Programming

Transcript

  1. Model View Controller • Trygve Reenskaug Computerworld. (2015). Trygve Reenskaug.

    http://www.cw.no/artikkel/rosings-hederspris-til-forsker Hopkins, J.M. (2016). Commodore PET Computer. https://hackaday.io/project/9950-commodore-pet-usb-keyboard
  2. Model View Controller • Trygve Reenskaug • ~1979 Computerworld. (2015).

    Trygve Reenskaug. http://www.cw.no/artikkel/rosings-hederspris-til-forsker Hopkins, J.M. (2016). Commodore PET Computer. https://hackaday.io/project/9950-commodore-pet-usb-keyboard
  3. Model View Controller • Trygve Reenskaug • ~1979 • Computerworld.

    (2015). Trygve Reenskaug. http://www.cw.no/artikkel/rosings-hederspris-til-forsker Hopkins, J.M. (2016). Commodore PET Computer. https://hackaday.io/project/9950-commodore-pet-usb-keyboard
  4. Model View Controller … UIViewController Wikimedia Commons. (2015). Swift Logo.

    https://commons.wikimedia.org/wiki/File:Swift_logo.svg Masilotti, J. (2015). UI Example. http://masilotti.com/ui-testing-cheat-sheet/
  5. 3 551 Model View Controller … UIViewController Wikimedia Commons. (2015).

    Swift Logo. https://commons.wikimedia.org/wiki/File:Swift_logo.svg Masilotti, J. (2015). UI Example. http://masilotti.com/ui-testing-cheat-sheet/
  6. 2 367 Model View Controller … UIViewController Wikimedia Commons. (2015).

    Swift Logo. https://commons.wikimedia.org/wiki/File:Swift_logo.svg Masilotti, J. (2015). UI Example. http://masilotti.com/ui-testing-cheat-sheet/
  7. 2 367 Massive View Controller … UIViewController Wikimedia Commons. (2015).

    Swift Logo. https://commons.wikimedia.org/wiki/File:Swift_logo.svg Masilotti, J. (2015). UI Example. http://masilotti.com/ui-testing-cheat-sheet/
  8. Clean Architecture • Robert C. Martin— Uncle Bob. Koss, B.

    (2009). Robert C. Martin. https://www.flickr.com/photos/koss/3250213001/
  9. Clean Architecture • Robert C. Martin— Uncle Bob. • 2012

    Koss, B. (2009). Robert C. Martin. https://www.flickr.com/photos/koss/3250213001/
  10. Use Case • Data: • <Client-ID> • Priority path: 1.

    User enters Client-ID and hits the Balance button. 2. System obtains client’s balance value from the database. 3. System shows balance to User. • Exception path: • User enters wrong Client-ID. Check client’s balance
  11. Clean Architecture @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output output

    output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController
  12. Clean Architecture @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output output

    output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController
  13. Clean Architecture @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output output

    output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController
  14. Unidirectional Data Flow @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController Balance
  15. Unidirectional Data Flow @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController Balance Request
 clientId=7
  16. Unidirectional Data Flow @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController Balance Request
 clientId=7
  17. Unidirectional Data Flow @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController Balance Request
 clientId=7 Response 1234
  18. Unidirectional Data Flow @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController Balance Request
 clientId=7 Response 1234 ViewModel
 “$1.234”
  19. Unidirectional Data Flow @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController Balance $1.234 Request
 clientId=7 Response 1234 ViewModel
 “$1.234”
  20. Dependencies? @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output output output

    Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController
  21. Peripherals for Interactor! @protocol DatabaseCRUD @protocol InteractorOutput UIWindow Database output

    output output Configurator First implementation in Swift by Raymond Law, clean-swift.com Presenter View PresenterOutput Router @protocol InteractorInput Interactor ViewController
  22. Clean Architecture Advantages • Most important code is in Interactor

    that implements the Use Case. • ViewController contains ~200-400 lines. • All code is 100% testable. • Easy to add/change features.
  23. Data is passed by routers UIWindow Navigation Controller Router output

    View Controller output View Controller Router Object UUID Interactor Interactor
  24. Links • MVC • heim.ifi.uio.no/~trygver/1979/mvc-2/1979-12- MVC.pdf • Clean Architecture •

    8thlight.com/blog/uncle-bob/2012/08/13/the- clean-architecture.html • www.youtube.com/watch?v=o_TH-Y78tt4