iOS face questions: the difference between MVC and MVVM

The difference between MVC and MVVM

1. MVC

MVC

MVC drawbacks

  • Heavy View Controller

    • M: based on the model of the object is usually very simple. The Apple document, model data include the business logic and data operations. In practice, the model layer is often very thin, in any case, the business logic layer model should not be dragged to the controller.

    • V: view view typically UIKit control (component, according to the control diet translated here) set or the coding control UIKit defined. View of how to build (PS: IB or handwriting interface) so why let the Controller know, while View should not directly reference model (PS: In reality, you know!), And only by reference controller IBAction event. Business logic is obviously not included in view, the view does not have any business.

    • C: Controller controller. Controller app is the "glue code": all interactions between the coordination model and the view. The controller manages the view hierarchy view of their own, but also in response to loading view, appearing, disappearing, etc., and often also full of model logic model and we do not want to expose business logic do not want to expose to the view . Requesting network data and subsequent processing, a local database operations, as well as some properties of the auxiliary tool with the methods have increased the generation of Massive View Controller.
  • Lost (nowhere to place) network logic
    is defined using MVC Apple says so: all objects can be categorized as a model, a view, or a controller.

You might try it in the Model objects inside, but also very difficult, because the network should use an asynchronous call, so if a network request to hold than its model life cycle is longer, things will become complicated. Clearly View network requests which do it more out of tune, so only the Controller. If so, this has exacerbated the problem of Massive View Controller. If so, where is the logic of the network home?

  • Poor testability

Because View Controller mixed view processing logic and business logic unit test separation of these components has become a difficult task.

2. MVVM

A way a good solution Massive View Controllerto the problem is to show the logic Controller extracted, placed in a special place, and this place is viewModel. MVVM derived MVC, it is an evolution of the MVC, which facilitates the separation of business logic and UI code. It is the nature of formal specification views and controls tightly coupled, and the introduction of new components. Structural relationships between them are as follows:

MVVM

The basic concept of 2.1 MVVM
  • In the MVVMmiddle, viewand view controllerofficially together, we see them as a component
  • viewAnd view controllerit can not be referenced directly model, instead of referring to the view model ( viewModel)
  • viewModel Placing a user input validation is logical, logical view display, the originating network code request and other places
  • Use MVVMa slight increase in the amount of code, but it reduces the overall complexity of the code
2.2 MVVM Notes
  • viewReference viewModel, but not vice versa (ie, not in viewModelthe introduction of #import UIKit.h, any reference view itself should not be placed viewModelin) (PS: the basic requirements that must be met )
  • viewModelReference model, but not vice versa * MVVM's recommendations
  • MVVMIt can be compatible with your current use of MVCarchitecture.
  • MVVM Increase your testability applications.
  • MVVMWith a binding mechanism works best (PS: ReactiveCocoa you deserve).
  • viewControllerTry not to involve the business logic, so viewModeldo these things.
  • viewController 只是一个中间人,接收 view 的事件、调用 viewModel 的方法、响应 viewModel 的变化。
  • viewModel 绝对不能包含视图 view(UIKit.h),不然就跟 view 产生了耦合,不方便复用和测试。
  • viewModel之间可以有依赖。
  • viewModel避免过于臃肿,否则重蹈Controller的覆辙,变得难以维护。
2.3 MVVM 的优势
  • 低耦合:View 可以独立于Model变化和修改,一个 viewModel 可以绑定到不同的 View
  • 可重用性:可以把一些视图逻辑放在一个 viewModel里面,让很多 view 重用这段视图逻辑
  • 独立开发:开发人员可以专注于业务逻辑和数据的开发 viewModel,设计人员可以专注于页面设计
  • 可测试:通常界面是比较难于测试的,而 MVVM 模式可以针对 viewModel来进行测试
2.4 MVVM 的弊端
  • 数据绑定使得Bug 很难被调试。你看到界面异常了,有可能是你 View 的代码有 Bug,也可能是 Model 的代码有问题。数据绑定使得一个位置的 Bug 被快速传递到别的位置,要定位原始出问题的地方就变得不那么容易了。
  • 对于过大的项目,数据绑定和数据转化需要花费更多的内存(成本)。主要成本在于:
  • 数组内容的转化成本较高:数组里面每项都要转化成Item对象,如果Item对象中还有类似数组,就很头疼。
  • 转化之后的数据在大部分情况是不能直接被展示的,为了能够被展示,还需要第二次转化。
  • Only when the API returns data in a highly standardized, these objects prototype ( Item) can be a high degree of reuse it, or type of explosion prone, increase maintenance costs.
  • By viewing the data content of the object prototype debug than directly through NSDictionary/NSArrayintuitive.
  • When the data of the same API are showing different View, difficult to control code data conversion, they may be scattered wherever needed.
3. Summary
  • MVCDesign pattern and it is not terminally ill, incurable architecture, at least currently MVC design pattern is still the mainstream of iOS development framework, there is reasonable. Articles for the drawbacks, we still have a lot of possible ways to avoid and resolve, thus creating a lightweight ViewController.

  • MVVMIs MVCan upgraded version is fully compatible with the current MVC architecture, MVVM although promoted to reduce the separation, to a certain extent UI code and business logic on ViewControllerthe degree of bloated, but Viewand ViewModeldata between the binding makes MVVM become complex and difficult to use data if we can not manage better between the two bindings, it will also cause Controller code is too complex, difficult to maintain code logic problems.

  • A lightweight ViewControlleris based MVCand MVVMthe separation of duties model code and build. MVC and MVVM has advantages and disadvantages, but the drawback worth mentioning in front of the benefits they bring when. Their low coupling, encapsulation, testability, maintainability, and many people work together to facilitate open method greatly improves efficiency.

  • At the same time, we need to maintain an embrace change hearts and attitudes of rational analysis. In the face of new technologies, not blind obedience, nor conservative, all decisions should be based on careful analysis, in order to respond to changes in technology.

More: iOS face questions Collection

Guess you like

Origin blog.51cto.com/14544867/2438038