MVVM pattern consists of three layers:
1、Model: App data that the app operates on.
2、View: The user interface’s visual elements. In iOS, the view controller is inseparable from the concept of the view.
3、ViewModel: Updates the model from view inputs and updates views from model outputs.
MVVM some advantages over MVC
1、Reduced complexity: MVVM makes the view controller simpler by moving a lot of business logic out of it.
2、Expressive: The view model better expresses the business logic for the view.
3、Testability: A view model is much easier to test than a view controller. You end up testing business logic without having to worry about view implementations.
MVVM Roles and Responsibilities
1、Model inputs: Taking view inputs and updating the model.
2、Model outputs: Passing model outputs to the view controller.
3、Formatting: Formatting model data for display by the view controller.
Data Binding
MVVM need a way to bind view model outputs to the views.
To do that, you need a utility that provides a simple mechanism for binding views to output values from the view model. There are several ways to do such bindings:
1、Key-Value Observing or KVO: A mechanism for using key paths to observe a property and get notifications when that property changes.
2、Functional Reactive Programming or FRP: A paradigm for processing events and data as streams. Apple’s new Combine framework is its approach to FRP. RxSwift and ReactiveSwift are two popular frameworks for FRP.
3、Delegation: Using delegate methods to pass notifications when values change.
4、Boxing: Using property observers to notify observers that a value has changed.
网友评论