MVC (design mode) at the top of iOS

Source: Internet
Author: User

MVC (design mode) at the top of iOS

MVC(Model-View-Controller, Model-View-Controller) is a software architecture Model in software engineering. It divides the software system into three basic parts: Model) view and Controller ).

MVC is not a Design Pattern, but an Architectural Pattern used to describe the structure of an application and the responsibilities and interactions of each part of the structure. It was first proposed in 1979 for the first time. However, the environment was somewhat different and the concept of network application did not exist at the time.

Tim Berners-Lee planted the seeds of the World Wide Web (WWW) in the early 1990s S and changed the world forever. Currently, the model we use in network development is actually an adapted version of the original model. The crazy popularity of this architecture mode is because two extremely popular development frameworks include this mode: Struts and Ruby on Rails. These two development frameworks are deeply imprinted on the hundreds of frameworks that will be born later.

1. Three-tier MVC Architecture

Model(Model): a data Model is used to encapsulate data related to the application's business logic and to process the data. The model has the right to directly access data, such as accessing the database. "Model" does not depend on "View" and "controller", that is, the model does not care about how it is displayed or operated. However, data changes in the model are generally published through a refresh mechanism. To implement this mechanism, the views used to monitor this model must be registered on this model in advance, so that the views can understand the changes in the data model. (Comparison: Observer mode in software design patterns)

View(View): The View layer can display data purposefully (theoretically, this is not necessary ). Generally, there is no program logic in the view. To refresh the view, the view needs to access the data model it monitors. Therefore, you should register the data that it monitors in advance.

Controller(Controller): The Controller acts as an organizational unit between different layers and is used to control application processes. It processes events and responds. "Events" include user behaviors and changes in data models.

 

  • Controller-forwards requests and processes the requests.
  • View-the GUI designer designs the GUI.
  • Model-programmers compile the functions (Implementing algorithms, etc.) of programs and database experts to manage data and design databases (implementing specific functions ).

     

    2. MVC principles

    The idea behind the MVC (Model-View-Controller) architecture mode is very simple. Our applications must differentiate the following responsibilities:

     

    Applications are divided into three main parts, each of which is responsible for different tasks.

     

    3. MVC network applications

    Controller(Controller)

    The Controller is in charge of user requests (when a user clicks an element on the graphic user interface (GUI) to perform operations, the Controller will receive an http get or POST request ). Its main function is to call and coordinate required resources/objects to execute user requests. Generally, the Controller calls an appropriate model for the task and selects an appropriate view.

    Model(Model)

    A model is a data rule and data content applied to data. It generally corresponds to objects to be managed by applications. In a software system, everything can be abstracted into a data model that can be processed in a certain way. What are the users, information, and books in the application? They are just a bunch of data that must be processed according to the corresponding rules (the date cannot be a future date, the email has a specific format, and the name length cannot exceed many characters ).

    The model provides the Controller with a data expression (such as information, books, and album) corresponding to the user request content ). No matter how we present it to users, this data model will not change. This is why we can choose which view to use to display data at will. The model contains the most important components of our application logic, which are applied to the problem we are dealing. The controller contains the internal organizational logic of the application.

    View(View)

    The view provides different ways to display model data. It may be a data filling template. There can be multiple views, and the Controller determines which view to use. A network application usually consists of many controllers, models, and views. A controller can be considered as a master controller that receives all user requests and then calls a specific controller to handle different situations.

     

    4. Advantages of MVC

    One of the most obvious advantages of MVC is that it clearly separates View display from application logic.

    Support for different users and different device types has always been a common problem. For example:

    The view obtained for requests from desktops and mobile phones should be different, and the model will return identical data, however, the difference is that the controller selects a View File to display data (we can regard it as a different template ).

    In addition to separating the view from the business logic, the separation of MVC also reduces the difficulty of large-scale application design, and the code is more structured, so it is easier to maintain, test, and reuse.


    5. MVC application scenarios

    The disadvantage of the MVC mode is that it is not easy to fully understand the MVC mode because it is not clearly defined. Using the MVC pattern requires careful planning. Because of its complicated internal principles, it takes some time to think about it. Developing an MVC-mode architecture project will have to spend considerable time considering how to apply the MVC-mode to applications. At the same time, 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. In addition, because the MVC mode divides an application into three parts, this means that the same project will contain more files than before.
    The MVC pattern is not suitable for small or even medium-sized applications, which brings in additional workload and increases application complexity. However, the current multi-data design framework can directly provide the MVC skeleton for the development of small and medium-sized applications. This problem no longer exists. For large-scale applications with a large number of user interfaces and complex logic, MVC will bring a new step to the software in terms of robustness, code reuse, and structure. Although the initial construction of the MVC Framework requires a certain amount of work, it will greatly improve the efficiency of software development in the long run.

     

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.