Differences between MVVM and MVC
1. MVC
The disadvantages of the MVC
- Heavy View Controller
- M: The object of the model is usually very simple. According to Apple’s documentation, the Model should include the data and the business logic that manipulates the data. In practice, the Model layer tends to be very thin, and the business logic of the Model layer should not be dragged into the Controller anyway.
- A view is usually a collection of UIKit controls (Component) or encoded UIKit controls. There is no need to let Controller know how the View is constructed (PS: IB or handwriting interface), and the View should not refer directly to the Model (PS: real life, you know!). And reference the Controller only through the IBAction event. Business logic is clearly not subsumed in a View; the view itself has no business.
- C: controller Controller. The Controller is the app’s “glue code” : coordinating all interactions between the model and view. Controllers manage view hierarchies of the views they own and respond to loading, chatting, baying, etc. They are often filled with model logic of models we don’t want to expose and business logic we don’t want to expose to views. Requests for and subsequent processing of network data, local database operations, and some instrumental assistance methods all augment Massive View Controller generation.
- Missing network logic The MVC definition used by Apple says that all objects can be classified as a Model, a View, or a Controller.
You might try to put it in a Model object, but that can be tricky because network calls should be asynchronous, so if a network request lasts longer than the Model that holds it, things get complicated. Obviously, it’s even more out of place to do network requests in a View, so you’re left with Controller. If so, this exacerbates the Massive View Controller’s problems. Otherwise, where is the home of network logic?
- Poor testability
Because the View Controller is a mix of View processing logic and business logic, unit testing to separate these components is a daunting task.
2. MVVM
A good way to solve the Massive View Controller problem is to extract the presentation logic from the Controller and place it in a special place, the viewModel. Derived from MVC, MVVM is an evolution of MVC that facilitates the separation of UI code from business logic. It formalizes the tightly coupled nature of views and controllers and introduces new components. The structural relationship between them is as follows:
2.1 Basic Concepts of MVVM
- in
MVVM
,view
和view controller
Formally linked together, we treat them as one component view
和view controller
Neither can be quoted directlymodel
, instead referring to the viewmodel (viewModel
)viewModel
Is a place to put user input validation logic, view display logic, initiate network requests, and other code- use
MVVM
This slightly increases the amount of code, but overall reduces the complexity of the code
2.2 Precautions for MVVM
view
referenceviewModel
, but not the other way aroundviewModel
The introduction of#import UIKit.h
Any reference to the view itself should not be placedviewModel
(PS:Basic requirements must be met)viewModel
referencemodel
, but not the other way around * MVVM usage recommendationsMVVM
It’s compatible with what you’re currently usingMVC
Architecture.MVVM
Increase the testability of your application.MVVM
It works best with a binding mechanism (PS:ReactiveCocoaYou deserve it.viewController
Try not to involve business logic, letviewModel
Do these things.viewController
Just a middleman, receivingview
Event, callviewModel
The method and response ofviewModel
The change.viewModel
You must not include viewsThe view (UIKit. H)
“Or withview
This creates coupling, which is not easy to reuse and test.viewModel
There can be dependencies.viewModel
Avoid being too bloated or repeat yourselfController
Is difficult to maintain.
2.3 Advantages of MVVM
- Low coupling:
View
Can be independent ofModel
Changes and modifications, oneviewModel
Can be bound to differentView
上 - Reusability: You can put some view logic into one
viewModel
Inside, let a lotview
Reuse this piece of view logic - Independent development: Developers can focus on business logic and data development
viewModel
Designers can focus on page design - Testable: Interfaces are often difficult to test, while
MVVM
Patterns can be targetedviewModel
To test it out
2.4 Disadvantages of MVVM
- Data binding enables
Bug
It’s hard to debug. You saw something wrong with the interface. It could be youView
The code isBug
Or it could beModel
There is a problem with the code. Data binding makes a locationBug
Being quickly transferred to other locations, it becomes less easy to locate the original problem. - For large projects, data binding and data transformation require more memory (cost). The main costs are:
- The cost of converting the contents of an array is high: each item in the array needs to be converted to
Item
Object, if you have an Item object that looks like an array, it’s a headache. - In most cases, the transformed data cannot be displayed directly. In order to be displayed, a second transformation is required.
- Only when the data returned by the API is highly standardized can these object prototypes (
Item
) is highly reusable, otherwise prone to type explosion, increase maintenance costs. - Viewing the data content through object prototypes is not as good as viewing it directly during debugging
NSDictionary/NSArray
Intuitive. - When data from the same API is presented by different views, it is difficult to control the code that converts the data, and it can be scattered wherever it is needed.
3. Summary
MVC
Design mode is not a terminally ill, incurable architecture, at least at present MVC design mode is still the mainstream framework of iOS development, its existence is reasonable. There are still many possible ways to avoid and solve the drawbacks described in this article, and build a lightweightViewController
.MVVM
isMVC
While MVVM promotes the separation of UI code from business logic, it is somewhat mitigatedViewController
Is bloated, butView
andViewModel
The data binding between the two makes MVVM complicated and difficult to use. If we can’t better manage the data binding between the two, it will also cause the problem that the Controller code is too complex and the code logic is difficult to maintain.- A lightweight
ViewController
Is based onMVC
andMVVM
Patterns are built for the separation of code responsibilities. MVC and MVVM have both advantages and disadvantages, but the disadvantages are insignificant when compared to the benefits they bring. Their low coupling, encapsulation, testability, maintainability and multi-person collaboration greatly improve the efficiency of the method. - At the same time, we need to maintain a heart to embrace change, and rational analysis of the attitude. In the face of new technology, we should not blindly follow or be old-fashioned. All decisions should be based on careful analysis so that we can cope with technological changes.
More: a collection of iOS Interview questions