Misunderstood MVC and the deified MVVM

Source: Internet
Author: User

iOS development apps are generally based on the Swift language, but some features also involve objective-c. There's been a lot of debate about MVC , and my point is : for IOS Most of the scenarios in development , MVC itself is no problem , you think the problem of MVC , must be your own understanding of the problem .

MVC and MVP in IOS

In short , the MVP is view -driven , and the view layer holds a reference to the corresponding Presenter , The interaction event on the View first invokes The interface provided by Presenter , and then Presenter invokes The method provided by the Model to obtain the data , and finally Presenter pass the obtained data to the View Show on .

MVC is driven by controller , controller holds View, and responds to the interactive events on the View , and gets the feedback data based on the interaction call different Model methods , then pass the data to View Show .

My understanding is that MVP is the user 's point of view : What is seen as View.mvc is the programmer's point of view : I control everyone.

The difference between understanding MVC and MVP is that Uiviewcontroller is a C (P) layer or V ? The following will be a specific analysis of these two views .

Point one : Uiviewcontroller 's Attribution --->view

If the Uiviewcontroller is treated as a V-layer, the above MVP Passive view, then uiviewcontroller will only be responsible for the view layout-related logic , No interaction with the Model layer is involved!

No interaction with the Model layer is involved!

No interaction with the Model layer is involved!

All business logic interactions through Uiviewcontroller held by Presenter and Model To complete the call between the two .

Point two : Uiviewcontroller 's Attribution --->controller

Well, if you think of Uiviewcontroller as a C - layer , from the MVC design Concept , , C layer will not be responsible for specific View the layout and presentation logic , but because IOS in Uiviewcontroller the Special design , causes many developers to directly Uiviewcontroller contains a lot of specific layout-related code , the more frightening situation is that more than just View Initialization of , including network requests and specific business processes are also included in the Uiviewcontroller in , Maybe it's someone who joked MVC to be : Massiveviewcontroller the cause .

Model,datainfo and the controversy over the fat skinny Model

the difference between Model and datainfo

The MVC architecture idea tends to be that the Model is a Layer rather than an Object (Java or The bean in Android ).

So here's datainfo I define it as a DTO (Data Transfer objec), and more simply , is a data structure , study with us at school C language when written in a Student structure is basically a meaning .

My understanding is that Model is used to handle business logic and can be seen as the BLL (bussiness logic Layer) in the traditional development three-tier architecture and the DAL (Data Access Layer) , which is responsible for all the specific business . like , for a security-certified App, you may need a Authmodel to be responsible for all authentication logic and local persistence of authentication information . this , Controller only need to call the Authmodel provides the interface to complete the corresponding security authentication function , Clear Responsibilities .

some of the methods in Model output dtos that are used to update the View. For example Usermodel will go to query user information , the user information returned by the server is then converted into a local UserInfo, put this UserInfo passed to View to show .

As a result of the industry's controversy over the fat-thin Model , I am more aware of the controversy over whether Datainfo needs to provide extended functionality related to the business . To learn more about the actual project, you can click: iOS video tutorial

(http://www.maiziedu.com/course/ios/)

Examples of business scenarios

For example a business scenario , A user information View needs to show the user's gender , In general, the server will only include a sex field in the returned user information ( 0 for female , 1 On behalf of men ), You may need to use the if statement to determine and then output a different gender text or picture .

In personal terms , Many developers convert the user information returned by the server into a local UserInfo dto, and then the dto Pass to the view that you want to display , and then make the output judgment in view .

Of course , developers may use some dictionaries that are now popular to model frameworks (yymodel,mjextension , etc. ), You can also use the configuration interfaces provided by these frameworks to transform this output logic at the time of conversion , or directly in the uerinfo sex Property The Getter method is converted after the output .

In any case , as long as These transformations are done at the datainfo level , the business logic has penetrated DTO the definition .

A solution to the idea

However , This scenario is almost inevitable , How to solve it ? be misunderstood. MVC and the deified MVVM in this paper, a reference MVVM the solution of the idea , the specific approach is to Viewcontroller Give View the process of passing data is abstracted into a structure ViewModel the process . after this abstraction, View only Accept ViewModel , while Controller only need to pass ViewModel such a line of code. In addition to the process of constructing ViewModel , we can move to another class .

In my opinion , this ViewModel layer actually just extracts the extension of Datainfo from above alone . this will View layer is completely isolated from the business logic .

By contrast, MVC has been able to meet The basic requirements of iOS development refactoring, and in the next article we will give an example to illustrate this problem.

Misunderstood MVC and the deified MVVM

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.