The advantages and disadvantages of iOS learning MVC,MVVM,MVP mode

Source: Internet
Author: User

Why pay attention to architecture design?

Because if you don't care about architecture, then one day you need to debug a few complex things in the same huge class, and you'll find that under such conditions, It is not possible to quickly find and effectively modify any bug in this class. Of course, it is difficult to think of such a class as a whole, so it is possible that some important details will always be overlooked in this process.

Analyze the pros and cons of three modes:

MVC is Modal view Controller (model view controllers). The 1980s a software design pattern invented for programming language Smalltalk-80

Some of the obvious features and embodiment of MVC:

    1. Task averaging--view and model are really separate, but the view and controller are tightly coupled
    2. Testability-the model can only be tested due to poor dispersion
    3. Ease of use-the smallest amount of code compared to several other modes. Many people are familiar with it, so it's easier to maintain it even for less experienced developers.
    4. If you don't want to devote more effort to architectural choices, then cocoa MVC is undoubtedly the best solution, and you'll find that some other high-maintenance models are a deadly blow to the small applications you've developed.

In terms of development speed, Cocoa MVC is the best architecture option. ”

    1. What is shown above the View depends on the Model. As long as the Model data is changed, the display status of the View is changed accordingly. Control is responsible for initializing the model and passing the model to the View to parse the display.
    2. MVC is an authoritative paradigm for organizing code and a standard model for building IOS apps. That's even what Apple says. Under MVC, all objects are categorized as a model, a view, or a controller. The model holds the data, the view shows the interface that interacts with the user, and the view Controller mediates the interaction between the model and the view.

There are many deficiencies in MVC itself.

1) Lack of MVC in real-world applications:

In MVC mode, view notifies the controller of user interaction. The view Controller responds to state changes by updating the Model. Model (usually using key-value-observation) notifies the controller to update the view that they are responsible for. Most IOS application code is organized in this way.

2) The increasingly cumbersome Controller:

In traditional apps, model data is generally simple, does not involve complex business data logic processing, client development is limited to its own running platform terminal, which is destined to the mobile end of the PC as the front-end can handle a large number of complex business scenarios. However, with the various in-depth of the mobile platform, we have to consider this problem. The traditional Model data mostly comes from the network data, after getting the network data, the client has to do is to draw the data directly in the order of the interface. As the business becomes more and more deep, the service services we rely on may not be able to meet the needs of the client for the first time in most of the time, and the mobile will have to handle a part of the logical computing operations on its own. This time the usual practice is to handle in the controller, resulting in the controller into a dustbin, more and more can not be maintained.

Controller controllers are the "glue codes" of the app that coordinate all interactions between the model and the view. The controller is responsible for managing the view hierarchy of the views they own, as well as responding to views of loading, appearing, disappearing, and so on, often with model logic that we don't want to expose, and business logic that doesn't want to be exposed to the view. This leads to the first question about MVC ...

Views view is typically a collection of UIKit controls (component, which are interpreted as controls here), or UIKit controls that are defined by encodings. Enter. Xib or Storyboard will find that an app, Button, and Label are made up of these visually and interactively controlled controls. View should not refer directly to Model, and only use the Ibaction event to reference the controller. The business logic clearly does not fall into view, and the view itself does not have any business.

Heavy View controllers have become rather bloated due to the large amount of code being put into Viewcontroller. There are hundreds of thousands of lines of code in the view controller in IOS that have never been seen before. Highlights of these overweight apps include the fact that heavy View controllers are difficult to maintain (due to their sheer size), contain dozens of attributes, make their state difficult to manage, and follow many protocols (protocol), resulting in protocol response codes and Controller Are confused with the logic code.

Heavy view controllers are difficult to test, either manually or using unit tests because there are too many possible states. Breaking the code down into smaller modules is usually a good thing.

3) Too lightweight Model:

The early model layer, in fact, if the data has a few properties, define a few properties, ARC popularization after we in the Model layer of the implementation of the file basically do not see the code (no need to manually manage the release of variables, Model neither complex business processing, nor the construction of objects, basically. m The code in the file is generally empty), and the stronger the contrast with the controller's code, which once made people wonder about the existing development design ideas.

4) Lost network logic:

The definition of MVC used by Apple is that: all objects can be categorized as a Model, a view, or a controller. So, where do you put the network code? Where should the code that communicates with an API be placed?

You might try to put it in the Model object, but it can be tricky, because a network call should use Asynchrony, so if a network request is longer than the model life cycle that holds it, things will become complicated. Obviously, the network code should not be placed in the view, so only the controller is left. This is also a bad idea, because it exacerbates the problem of heavy controllers. So where should we put it? It is clear that the 3 components of MVC have no place to put the code at all.

5) Poor testability

Another big problem with MVC is that it does not encourage developers to write unit tests. Because the controller mixes the view processing logic with the business logic, unit testing that separates these components is a daunting task. Most people choose to ignore this task by not doing any tests.

As mentioned above, the controller can manage the hierarchy of views, the controller has a "view" property, and can access any child views of the view through Iboutlet. This is not easy to extend when there are many outlet, and in a sense it is best not to use a child view controller to help manage the child views. There are a number of vague criteria here, and no one seems to be able to agree completely. In any case, the view and the corresponding controller are tightly coupled, in short, they will be treated as a component. This component that Apple has provided has misled most beginners to some extent, with beginners dragging all views to xib, connecting a large number of Iboutlet export properties, all of which are column issues.

MVVM: Modal View ViewModel (Model View view model). Presented by Microsoft's WPF and Silverlight architect John Gossman in 2005

Several obvious features and embodiment of the MVVM pattern

    1. The task is equally--MVVM's view is more responsible than the view in MVP. Because the former updates the state with the ViewModel set binding, the latter listens only to Presenter events but does not update themselves.
    2. Testability-ViewModel does not know anything about the View, which allows us to easily test the ViewModel. View can also be tested, but due to the category of UIKit, their tests are often ignored.
    3. Ease of use-in real-world development, you must point the event in the view to Presenter and manually update the view, and the amount of MVVM code will be much smaller if binding is used.
    4. The MVC view controller is too large and difficult to manage, and this pattern provides a good alternative to MVC, which guarantees the lightweight of the view controller.

MVP, Modal View Presenter (Model View coordinator), probably appeared in 2000, a variant of the MVC pattern.

John Gossman's criticism of MVVM has two main points:

    1. : Data binding makes it difficult to debug a Bug. You see the interface is abnormal, it is possible that your View code has a Bug, it may be the Model code has a problem. Data binding makes a Bug in one location quickly passed to another location, and it becomes less easy to locate the original problem.
    2. : For oversized projects, data binding takes more memory.

analysis of three features in MVP mode:

    1. The task is equally divided-we divide the most important tasks into Presenter and Model, and the View function is less;
    2. Testability-Very good, because a simple View layer, so test most of the business logic is also easy;
    3. Ease of use-the code is larger than the MVC pattern, but the concept of MVP is very clear.

The advantages and disadvantages of iOS learning MVC,MVVM,MVP mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.