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

Structuring Your Code With MVP

Sam Jarman
August 27, 2018

Structuring Your Code With MVP

MVC or Model View Controller has been the tried and true way for iOS programmers for years, but nowadays developers find it hard to test, structure and that it violates a few key architecture principles. Enter Model-View-Presenter, a much cleaner architecture that allows for separation of concerns, easier testing, and greater programmer happiness. This talk covers programming a standard iOS use case putting data from the server onto a screen, with the MVP pattern, plus a few other patterns to allow for maintainable and scalable iOS development.

Sam Jarman

August 27, 2018
Tweet

More Decks by Sam Jarman

Other Decks in Technology

Transcript

  1. /dev/world ’18 • @samjarman Who Am I? • iOS Developer

    at the Bank of New Zealand • iOS Dev since ages ago • Sabbatical in Ruby on Rails, EmberJS, AWS, more • Twitter friendly talk!
  2. /dev/world ’18 • @samjarman Ages in Age of Empires •

    Dark Age • Tool Age • Bronze Age • Iron Age
  3. /dev/world ’18 • @samjarman Model Controller View TableView CollectionView Customer

    Thing Object Network Caching Data Access / Persistence Control and coordinate View Datasources View Delegates
  4. /dev/world ’18 • @samjarman Model Controller View TableView CollectionView Customer

    Thing Object Network Caching Data Access / Persistence All the things…. View Datasources View Delegates
  5. /dev/world ’18 • @samjarman Controller • Delegate for TableView •

    Datasource for TableView • Network calls • Parsing of DTOs into models • Sorting of arrays • SQL/CoreData • Sizing cells • … more!
  6. /dev/world ’18 • @samjarman The Problem • Controllers doing too

    much • Almost no well defined inputs and outputs • Tonnes of side effects • No real consequences of this programmer behaviour
  7. /dev/world ’18 • @samjarman We Wanted… Products not experiments Longevity

    Apps as a strategy Readability Teams of developers Maintainability To use source control To release with confidence To keep up with OS updates To make good apps
  8. /dev/world ’18 • @samjarman Desirable Code • Single responsibility •

    Clear inputs and outputs • SOLID • Unit Testable • (… and a lot more!)
  9. /dev/world ’18 • @samjarman Good architecture is the act of

    decomposing your apps functionality into single responsibility objects with clear inputs and outputs
  10. /dev/world ’18 • @samjarman Possible Solutions • There are plenty

    of possible solutions and architectures for your app • MVP, MVVM, VIPER, More… • Lets chat about one today! Tuesday, 2:00 PM VIPER for iOS Thomas Verbeek
  11. /dev/world ’18 • @samjarman View • A standard iOS View

    • Could contain a tableview, collection view, stack views, whatever your heart* desires *pragmatic implementation of designs
  12. /dev/world ’18 • @samjarman View (Controller) • A view controller

    • Acts as delegate / datasource of TV/CV etc • Layout and populates views • Receives user actions • Asks and informs the presenter
  13. /dev/world ’18 • @samjarman Presenter • Interacts with Data provider

    classes • Instructs view what to do • Doesn’t know/care about the view
  14. /dev/world ’18 • @samjarman Provider • Reads/Writes to the source

    of truth • Server • Database • Other • Constructs models from that source • Passes models back to presenter
  15. /dev/world ’18 • @samjarman Model • A class or struct

    with properties • Represents an object • Maybe created by server response directly or Domain Transfer Object • Maybe failable init?
  16. /dev/world ’18 • @samjarman Presenter Provider ViewController View Model Network/Disk

    Delegate UIKits standard Method Call Delegate Method Call
  17. /dev/world ’18 • @samjarman Presenter Provider ViewController View Model Network/Disk

    Weak delegate Owns (Strong*) Owns(Strong) Weak delegate Owns (Strong) Constructs Owns/Singleton
  18. /dev/world ’18 • @samjarman Presenter Provider ViewController View Model Network/Disk

    Delegate UIKits standard Method Call Delegate Method Call
  19. /dev/world ’18 • @samjarman Unit testing • Clear inputs ✅

    - Interface • Clear outputs ✅ - Delegate/Protocol • Clear side affects ✅ - Only in provider! • Single responsibility ✅
  20. /dev/world ’18 • @samjarman –Me, excited about unit testing decent

    architecture “We can unit test the crap out of this thing”
  21. /dev/world ’18 • @samjarman Anatomy of a test • Setup

    / Given • Run / When • Verify / Then
  22. /dev/world ’18 • @samjarman And that’s MVP • Logical units

    of work • Clear inputs, clear outputs, no side effects • Possible to 100% unit test non-UI • Use of interfaces and delegates for communication
  23. /dev/world ’18 • @samjarman MVP is… • An option for

    you to structure your code • A light weight architecture for those new to architecting code
  24. /dev/world ’18 • @samjarman MVP is not.. • The only

    way to do this • A solution for all use cases, teams, and code bases • An end to all bugs • Noticeable to your users… for now
  25. /dev/world ’18 • @samjarman Architectural patterns are about expressing yourself

    in logical units that are individually replaceable, expandable, testable and interoperable
  26. /dev/world ’18 • @samjarman Thank you! • Me? www.samjarman.co.nz •

    Learn more about SOLID? https:// en.wikipedia.org/wiki/SOLID