IOS theoretical knowledge: MVC design mode and ios mvc design mode
One of the most frequently-used design patterns in iOS development should be the MVC design pattern. The full name of MVC is Model View Controller, short for model-view-controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.
1. Introduction to MVC
MVC originally exists in a desktop program. M refers to the business data model, V refers to the user interface view, and C refers to the Controller, the purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different expressions. For example, a batch of statistical data can be represented by a column chart, a pie chart, or a line chart. The purpose of C is to ensure synchronization between M and V. Once M changes, V should be updated synchronously.
Model-View-controller (MVC) is a software design pattern invented by Xerox PARC for programming language Smalltalk-80 in 1980s, which has been widely used. Later, it was recommended to be the design model of Oracle's Sun's Java EE platform, and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-controller mode is a useful toolbox, which has many advantages but also has some disadvantages.
Ii. MVC Mode
MVC is a pattern used to design and create a Web application using MVC (Model View Controller Model-View-Controller:
Model indicates the core of the program (such as the database record list ). View (View) displays data (database records ). The Controller processes input (written to database records ).
The MVC mode also provides full control over HTML, CSS, and JavaScript.
A Model is a part of a program that is used to process the data logic of an application. Typically, a Model object is used to access data in a database.
A View is a part of a program that processes data display. Generally, a View is created based on model data.
A Controller is a part of a program that processes user interaction. Generally, a Controller reads data from a view, controls user input, and sends data to the model.
MVC layering helps you manage complex programs, because you can focus on one of these aspects within a certain period of time. For example, you can focus only on the View Design without considering the business logic, which makes it easier to test applications.
MVC layering also simplifies group development. Different developers can develop views, controller logic, and business logic at the same time to facilitate collaborative development.
Iii. MVC Framework
MVC refers to a framework of the MVC pattern. It 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. The most typical MVC is the JSP + servlet + javabean mode.
1. Model:
The model represents the data and business rules of an enterprise. 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, such 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:
A view is an interface that you can see and interact. For older Web applications, a view is an interface composed of HTML elements. In New Web applications, HTML still plays an important role in the view, however, some new technologies are emerging, including Adobe Flash and some identification languages and Web services such as XHTML, XML/XSL, and WML. The advantage of MVC is that it can process many different views for applications. In fact, there is no real processing in the view. Whether the data is stored online or an employee list, as a view, it is just a way to output data and allow users to manipulate it.
3. Controller:
The Controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on the Web page and send an HTML form, the controller does not output or process anything. It only receives the request and determines which model component is called to process the request, and then determines which view is used to display the returned data.
Iv. Differences between the framework mode and the design mode
Many programmers often confuse the framework pattern with the design pattern and think that MVC is a design pattern. In fact, they are completely different concepts. The concepts of frameworks and design patterns are always confusing, but there are still differences between them. The framework is usually code reuse, while the design pattern is design reuse. The architecture is between the two. Some code reuse, some design reuse, and sometimes analysis can also be reused. There are three levels of reuse in software production: Internal reuse, that is, abstract blocks that can be commonly used in the same application; code reuse, which combines common modules into libraries or tool sets, it can be used in multiple applications and fields. the reuse of application frameworks provides general or ready-made infrastructure for specialized fields to achieve the highest level of reusability.
Although the framework is similar to the design model, it is fundamentally different. The design pattern is a description of the problem that occurs repeatedly in a certain environment and the solution to the problem. It is more abstract than the framework. The framework can be expressed in code and can be directly executed or reused, for patterns, only instances can be represented by code. design patterns are smaller elements than frameworks. A framework usually contains one or more design patterns, the framework always targets a specific application field, but the same pattern can be applied to various applications. It can be said that the framework is software, while the design mode is software knowledge.
The frameworks include MVC, MTV, MVP, CBD, and ORM;
Frameworks include QT, MFC, gtk in C ++, SSH, SSI in Java, smarty in php (MVC mode), django in python (MTV mode), etc.
The design modes include Singleton mode, factory mode, adapter mode, and Policy mode.
In short, the framework is a big framework used to divide software design. The design mode is a small model, and a solution is proposed for specific problems to improve code reuse rate and reduce coupling, that is to say, high cohesion and low coupling.
5. MVC features
(1) Advantages:
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.
The model is self-contained and separated from the Controller and view, so it is easy to change the data layer and business rules of the application. If you port a database from MySQL to Oracle, or change the data source from RDBMS to LDAP, you only need to change the model. Once the model is correctly implemented, the view correctly displays the data from the database or LDAP server. Since the three components of an application using MVC are independent of each other, changing one of them will not affect the other two, a well-coupled component can be constructed based on this design idea.
2. High reusability
As technology advances, more and more methods are needed to access applications. The MVC mode allows different views to access code on the same server, because multiple views share one model, including any WEB (HTTP) browser or wireless browser (wap). For example, you can use a computer or mobile phone to order a certain product. Although the ordering method is different, the ordering method 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. Since data and business rules have been separated from the presentation layer, code can be reused to the maximum extent. 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.
3. Low lifecycle costs
MVC reduces the technical content of developing and maintaining user interfaces.
4. Fast deployment
Using the MVC pattern, the development time is greatly reduced. It enables programmers (Java developers) to focus on business logic and interface programmers (HTML and JSP developers) focus on the form of expression.
5. high maintainability
Separating the view layer and business logic layer also makes WEB applications easier to maintain and modify.
6. Favorable 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. The Controller also provides the advantage of using controllers 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. (2) Disadvantages:
1. There is no clear definition
It is not easy to fully understand MVC. Using MVC requires careful planning. Because of its complicated internal principles, it takes some time to think about it. At the same time, due to the strict separation of models and views, it also brings some difficulties to debug the application. Each component must be thoroughly tested before use.
2. Not suitable for small and medium-sized applications
A large amount of time to apply MVC to a large-scale application is usually not worth the candle.
3. Increase the complexity of system structure and implementation
For a simple interface, strictly abide by MVC. Separating Models, views, and controllers increases the complexity of the structure and may lead to too many update operations to reduce the running efficiency.
4. Over-tight connection between views and controllers
Views and controllers are separated from each other, but they are closely related components. A view does not have a controller, and its applications are limited. Vice versa, which hinders their independent reuse.
5. Inefficient access of views to model data
Depending on the model operation interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also damage operation performance.
6. General Advanced Interface tools or constructors do not support mode
Modifying these tools to adapt to MVC needs and building separation components is costly and may cause difficulties in using MVC.