iOS Architecture Patterns Allan Shih
Agenda ● Model-View-Controller (MVC) ● Model-View-Presenter (MVP) ● Model-View-ViewModel (MVVM) ● Reference
A good architecture ● Balanced distribution of responsibilities among entities with strict roles. ● Testability usually comes from the first feature. ● Ease of use and a low maintenance cost.
Model-View-Controller ● A proven pattern for organizing code in complex application design ● Separates an application into three main logical components: model, view, and controller
Components in MVC ● Model ○ All the data-related logic that the user works with ● View ○ Presenting data to the user ○ Handling user interaction ● Controller ○ An interface between Model and View components
Realistic Cocoa MVC ● Cocoa MVC encourages you to write Massive View Controllers, because they are so involved in View’s life cycle that it’s hard to say they are separate.
Advantages of MVC ● Separation of Concerns ○ the View and the Model in fact separated, but the View and the Controller are tightly coupled. ● Testability  ○ you’ll probably only test your Model. ● Reusability ○ Controllers are often not reusable ○ View and model objects are easy to reuse ● The least amount of code among others patterns.
Problems of MVC ● MVC frameworks tend to lead to Apps with Massive View Controllers ● Network calls, persistence, view logic, and all other sorts of code ends up in the Controller. ● This Makes Controller code ○ difficult to read ○ difficult to change ○ difficult to unit test ○ difficult to reuse
Model-View-Presenter ● The Presenter implements an Observer design of MVC
Components in MVP ● Model ○ An interface defining the data to be displayed or otherwise acted upon in the user interface ● View ○ A passive interface that displays data ○ Routes user commands to the presenter ● Presenter ○ It retrieves data from repositories (the model), and formats it for display in the view
MVC and MVP
Features of the MVP ● Distribution  ○ responsibilities divided between the Presenter and the Model, with the View. ● Testability  ○  we can test most of the business logic due to the View. ● Easy of use  ○ the amount of code is doubled compared to the MVC, but at the same time, idea of the MVP is very clear. ● MVP in iOS means superb testability and a lot of code.
Model-View-ViewModel (MVVM) ● The MVVM treats the view controller as the View ● There is no tight coupling between the View and the Model
Components in MVVM ● Model ○ Used to represent a “model” of some set of data ● View ○ Used to represent the user interface (UI) of the app ● ViewController ○ Used to set up the UI of views and handle any user interaction with these views ● View Model ○ Used to represent a model of a view controller ○ Notifies the view of any state changes
Mapping of MVC and MVVM
iOS MVVM Architecture ● This leaves the view (controller) with a more clearly defined task of presenting the data supplied by the view-model. ● The view-model exposes the minimum information necessary to our view controller
Features of the MVVM ● Distribution  ○ data gathering / logic / transforming extracted away from the complexities of a view controller ● Testability  ○  the view model knows nothing about the View, this allows us to test it easily ● Easy of use  ○ When the view model needs to communicate something to the view, it does so through a system of data bindings
MVVM Disadvantages ● In bigger cases, it can be hard to design the ViewModel up front in order to get the right amount of generality. ● More files to serve the architecture. ● Some people think that for simple UIs, MVVM can be overkill. ● Debugging would be bit difficult when we have complex data bindings.
Summary
Data bindings ● Delegate ● Block Callbacks ● Target Action ● Key Value Observer (KVO) ● Notifications
Demo
Reference ● The Swift Programming Language (Swift 3.0.1) ● Model-View-Controller (MVC) in iOS: A Modern Approach ● iOS Architecture Patterns Demystifying MVC, MVP, MVVM and VIPER

iOS architecture patterns

  • 1.
  • 2.
    Agenda ● Model-View-Controller (MVC) ●Model-View-Presenter (MVP) ● Model-View-ViewModel (MVVM) ● Reference
  • 3.
    A good architecture ●Balanced distribution of responsibilities among entities with strict roles. ● Testability usually comes from the first feature. ● Ease of use and a low maintenance cost.
  • 4.
    Model-View-Controller ● A provenpattern for organizing code in complex application design ● Separates an application into three main logical components: model, view, and controller
  • 5.
    Components in MVC ●Model ○ All the data-related logic that the user works with ● View ○ Presenting data to the user ○ Handling user interaction ● Controller ○ An interface between Model and View components
  • 6.
    Realistic Cocoa MVC ●Cocoa MVC encourages you to write Massive View Controllers, because they are so involved in View’s life cycle that it’s hard to say they are separate.
  • 7.
    Advantages of MVC ●Separation of Concerns ○ the View and the Model in fact separated, but the View and the Controller are tightly coupled. ● Testability  ○ you’ll probably only test your Model. ● Reusability ○ Controllers are often not reusable ○ View and model objects are easy to reuse ● The least amount of code among others patterns.
  • 8.
    Problems of MVC ●MVC frameworks tend to lead to Apps with Massive View Controllers ● Network calls, persistence, view logic, and all other sorts of code ends up in the Controller. ● This Makes Controller code ○ difficult to read ○ difficult to change ○ difficult to unit test ○ difficult to reuse
  • 9.
    Model-View-Presenter ● The Presenterimplements an Observer design of MVC
  • 10.
    Components in MVP ●Model ○ An interface defining the data to be displayed or otherwise acted upon in the user interface ● View ○ A passive interface that displays data ○ Routes user commands to the presenter ● Presenter ○ It retrieves data from repositories (the model), and formats it for display in the view
  • 11.
  • 12.
    Features of theMVP ● Distribution  ○ responsibilities divided between the Presenter and the Model, with the View. ● Testability  ○  we can test most of the business logic due to the View. ● Easy of use  ○ the amount of code is doubled compared to the MVC, but at the same time, idea of the MVP is very clear. ● MVP in iOS means superb testability and a lot of code.
  • 13.
    Model-View-ViewModel (MVVM) ● TheMVVM treats the view controller as the View ● There is no tight coupling between the View and the Model
  • 14.
    Components in MVVM ●Model ○ Used to represent a “model” of some set of data ● View ○ Used to represent the user interface (UI) of the app ● ViewController ○ Used to set up the UI of views and handle any user interaction with these views ● View Model ○ Used to represent a model of a view controller ○ Notifies the view of any state changes
  • 15.
  • 16.
    iOS MVVM Architecture ●This leaves the view (controller) with a more clearly defined task of presenting the data supplied by the view-model. ● The view-model exposes the minimum information necessary to our view controller
  • 17.
    Features of theMVVM ● Distribution  ○ data gathering / logic / transforming extracted away from the complexities of a view controller ● Testability  ○  the view model knows nothing about the View, this allows us to test it easily ● Easy of use  ○ When the view model needs to communicate something to the view, it does so through a system of data bindings
  • 18.
    MVVM Disadvantages ● Inbigger cases, it can be hard to design the ViewModel up front in order to get the right amount of generality. ● More files to serve the architecture. ● Some people think that for simple UIs, MVVM can be overkill. ● Debugging would be bit difficult when we have complex data bindings.
  • 19.
  • 20.
    Data bindings ● Delegate ●Block Callbacks ● Target Action ● Key Value Observer (KVO) ● Notifications
  • 21.
  • 22.
    Reference ● The SwiftProgramming Language (Swift 3.0.1) ● Model-View-Controller (MVC) in iOS: A Modern Approach ● iOS Architecture Patterns Demystifying MVC, MVP, MVVM and VIPER