MVC design mode and iOSMVC design mode in iOS

Source: Internet
Author: User
Tags ldap

MVC design mode and iOSMVC design mode in iOS
When organizing code files for large projects, we often use MVC ideas. The concept of MVC is very simple, just like an object. However, understanding and application are very difficult. Today, we will briefly summarize the MVC design philosophy.

MVC (Model View Controller) model-view-controller ):
MVC originally exists in the Desktop program, M is the exponential data model, V is the user interface, and C is the controller. Using MVC separates the implementation code of M and V, so that the same program can use different expressions. For example, you can use a column chart or pie chart to represent a batch of statistical data. The purpose of C is to ensure the synchronization of M and V. Once M changes, V should be updated synchronously. From the example, we can see that MVC is a special case of the Observer design pattern.


MVC is a design pattern that forcibly separates the input, processing, and output of an application. MVC applications are divided into three core components: model, view, and controller. They process their own tasks. Layered Concept

(1) model object
The model object encapsulates the data of an application and defines the logic and operations for manipulating and processing the data. For example, the model object may be a role in the game or a contact in the address book. The Data creation or modification operations performed by the user in the view layer are communicated through the Controller object, and the model object will be created or updated. When a model object is changed (for example, new data is received through a network connection), it notifies the Controller object that the controller object updates the corresponding view object. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects such as EJBs and ColdFusion Components to process databases. The data returned by the model is neutral, that is, the model has nothing to do with the data format, so that a model can provide data for multiple views. Because the Code applied to the model can be reused by multiple views only once, code duplication is reduced.

 

(2) view object
View objects are visible to users in applications. The view object knows how to draw itself and may respond to user operations. The main purpose of a view object is to display data from an application model object and make the data editable. However, in MVC applications, view objects are usually separated from model objects. In iOS application development, all controls and windows are inherited from UIView, corresponding to V in MVC. UIView and its Child classes are mainly responsible for UI implementation, and events generated by UIView can be delegated to UIViewController for implementation.

 


(3) controller object
The Controller object acts as a medium between one or more view objects of an application and one or more model objects. The Controller object is therefore a synchronization pipeline program. Through this, the view object understands the changes to the model object, and vice versa. Controller objects can also perform setup and coordination tasks for applications and manage the lifecycles of other objects.

The Controller object interprets user operations performed on the view object and transmits new or changed data to the model object. When a model object is changed, a controller object transmits the new model data to the view object so that the view object can display it.


Why use MVC?
First, the most important thing is that multiple views can share a model. Now you need to access your applications in more and more ways. In this regard, one solution is to use MVC, whether your users want the Flash interface or WAP interface, they can be processed using a model. Since you have separated Data from business rules from the presentation layer, You can reuse your code to the maximum extent.
Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be represented in HTML, but they may also be represented in Adobe Flash and WAP. The model also provides State management and data persistence processing functions. For example, session-based shopping carts and e-commerce processes can also be reused by Flash websites or wireless networking applications.
Because the model is self-contained and separated from the Controller and view, it is easy to change the data layer and business rules of your application. If you want to port your database from MySQL to Oracle, or change your RDBMS-based data source to LDAP, you only need to change your model. Once you implement the model correctly, the view will display the data correctly regardless of whether the data comes from the database or the LDAP server. Because the three components of an application using MVC are independent of each other, changing one of them will not affect the other two, you can build a loose coupling component based on this design idea.
For me, the Controller also provides the advantage of using the Controller to connect different models and views to meet user needs. In this way, the controller can provide a powerful means to construct applications. Given some reusable models and views, the controller can select a model for processing based on the user's needs, and then select a view to display the processing results to the user.
Advantages of MVC
(1) low coupling
The view layer and business layer are separated so that you can modify the view layer code without re-compiling the model and controller code. Similarly, to change the business flow or rules of an application, you only need to change the MVC model layer. Because the model is separated from the Controller and view, it is easy to change the data layer and business rules of the application.


(2) High reusability and Applicability
As technology advances, more and more methods are needed to access applications. The MVC mode allows you to access the code on the same server using different views. It includes any WEB (HTTP) browser or wireless browser (wap). For example, you can use a computer or mobile phone to order a product. Although the ordering method is different, however, the method for handling product subscriptions is the same. Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be expressed in HTML, but may also be expressed in WAP. The commands required to represent these data are to change the implementation method of the view layer, the control layer and model layer do not need to be changed.


(3) Low Cost of Life Cycle
MVC reduces the technical content of developing and maintaining user interfaces.

 

(4) maintainability
Separating the view layer and business logic layer also makes the application easier to maintain and modify.


(5) conducive to Software Engineering Management
Because different layers perform their jobs, different applications have some same features, which is conducive to managing program code through engineering and tools.


Disadvantages of MVC:
The disadvantage of MVC is that it is not easy to fully understand MVC because it is not clearly defined. Using MVC requires careful planning. Because of its complicated internal principles, it takes some time to think about it.
You will have to spend a considerable amount of time thinking about how to apply MVC to your application, and because the model and view must be strictly separated, this also makes it difficult to debug the application. Each component must be thoroughly tested before use. Once your components have been tested, you can reuse them without any scruples.
Based on the developer's experience, because the developer divides an application into three parts, using MVC also means that you are going to manage more files than before, which is obvious. It seems that our workload has increased, but remember that this is not worth mentioning compared to the benefits it can bring us.
MVC is not suitable for small or medium-sized applications. It is usually not worth the candle if you spend a lot of time applying MVC to a small or medium-sized application.
The MVC design pattern is a good way to create software. It advocates some principles, such as separation of content and display, which may be better understood. However, if you want to isolate the components of models, views, and controllers, you may need to rethink your applications, especially the application architecture. If you are willing to accept MVC and have the ability to cope with the extra work and complexity it brings, MVC will put your software on a new level in terms of robustness, code reuse, and structure.

Ios mvc design mode:

 

In the figure, there are several lines that divide the three parts into yellow lines, dotted lines, and white solid lines. We think of them as road signs. You can see that there are two yellow lines between M and V. What does this mean? It means that you cannot cross this yellow line, and neither direction is possible, that is, M and V are completely separated. On the top of the graph, you can see the white dotted line, which means you can freely traverse it, as long as it is safe. What about the white solid line? It means you can pass through, but you must buy a ticket or pay a toll.

First, let's look at the Green Arrow between C and M. The direction of the arrow represents the direction of "initiating a dialogue", that is, C is initiating a dialogue, M is the answer. C can ask M various questions, but M only answers C's questions or requirements, and it cannot actively ask C for anything. Remember that the dotted line is unobstructed. So C knows everything about M. If code is used to explain it, C can import M header files or M interfaces (APIS ). Because C can use M's API, it can demand this requirement from M without authorization.

Let's take a look at another Green Arrow, which is between C and V. It has the same meaning as the previous Green Arrow. It means C can communicate directly with V. You can think about it. C wants to put V on the screen, set the V attribute, tell them when to disappear from the screen, and divide them into groups. If C cannot send a command to V freely, the display of the program will be very difficult. Therefore, C can speak to V without restrictions.

You may have noticed that there is an outlet (output port) on this arrow. outlet can be seen as a pointer from C to V, which is defined in C. Outlet provides us with great convenience, which enables us to easily and accurately execute orders to V within C. C can have more than one outlet, which makes it more efficient to communicate with V.

Can M and V communicate with each other? Do you still remember what the yellow line means? It is completely unavailable, so we do not allow M and V to communicate. This is because we don't want to have too much communication between these three parts. You can think about it. If V has a problem during display, for example, if a graph is not displayed, we have to look for an error, because C can communicate with V, and M can communicate with V, we need to check the two parts. On the contrary, if only C can communicate with V, when an error occurs, we only need to go to C to find the cause. Isn't it easy to find the error? Therefore, we do not allow direct connections between M and V, which is also the reason that there are two yellow lines between them. The following three points are summarized:

(1) Model and View cannot communicate with each other and can only be transmitted through the Controller.

(2) The Controller can directly communicate with the Model (read and write call Model). The Model indirectly communicates with the Controller through the Notification and KVO mechanisms.

(3) the Controller can directly talk to the View and operate the View through outlet. The outlet directly corresponds to the control in the View, view reports the occurrence of an event to the Controller through action (for example, user Touch me ). The Controller is a direct data source of the View (data is probably obtained from the Model and processed by the Controller ). The Controller is the View proxy (delegate) To synchronize the View with the Controller.


Next we will discuss how V sends messages to C. V has three different ways to communicate with C:

The first type is called target-action ).

It works in this way. C will "hover" a target in its internal structure, which is a red and white target. Corresponding, it will also distribute an action (action, to the view object (possibly a button on the screen) that will communicate with it. When the button is updated, the action will be sent to the corresponding target, in this way, V can communicate with C. However, in this case, V only knows whether to send an action to the corresponding target. It does not know the class in C or what it actually sends. Target-action is a common method.

The second method is called a delegate ).

Sometimes, V needs to be synchronized with C. You know, user interaction is not just about pressing a button, sliding a slider, but also in many forms. Let's take a look at the delegate yellow arrow in the figure. You can find four small arrows on the Arrow: shocould, did, And will. There is another one that is not marked. The vast majority of the delegate information is in the form of shocould, will, and did. Corresponding to the English meaning, shocould indicates that the view object will ask a certain object in C, "should I do this ?", For example, if someone clicks a link in a web View, the web View will ask "should I open this link? Is this safe ?". This is the account information. What about will and did? Will is "I am going to do this", did is "I have already done this ". C sets itself as the V delegate, which lets V know: If V wants to know more about how it will be displayed, it will send the delegate information to C. C will coordinate and process the delegate information sent by V. In addition, each V can have only one delegate.

The third method is the data source ),

V cannot have the data it wants to display. Remember this is very important. V wants others to help it manage the data to be displayed. When it needs the data, it will ask others for help and give it the required data. Furthermore, the iphone screen is small and cannot display views that contain a large amount of information. The datasource arrow in the figure is similar to delegate. V sends cout and data at information to C to request data.

For different uiviews, the corresponding UIViewController corresponds to C in MVC. For example, UITableView is commonly used on iOS, and its corresponding Controller is UITableViewController.

 

Related Article

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.