MVC (Model View Controller) originally exists in the Desktop program. M refers to the data model, V refers to the user interface, 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, you can represent a batch of statistical data using histograms and pie charts respectively. The purpose of C exists is to ensure the synchronization of M and V. Once M changes, V should be updated synchronously.
Model-View-Controller (MVC) is a software design model invented by Xerox PARC for the programming language Smalltalk-80 in the 1980s, and has been widely used to this day. In recent years, it has been recommended as the design model of Sun's J2EE platform and is popular with more and more developers using ColdFusion and PHP. Model-View-Controller Mode is a useful toolbox that has many benefits, but also some disadvantages.
How MVC works
MVC is a design pattern that forces the input, processing and output of an application to be separated. The MVC application is divided into three core components: model, view, and controller. They each handle their own tasks.
view
A view is the interface that the user sees and interacts with. For old-fashioned web applications, views are interfaces composed of HTML elements. In new-fashioned web applications, HTML still plays an important role in views, but some new technologies have emerged one after another, including Macromedia Flash, some identification languages such as XHTML, XML/XSL, WML, etc. and Web services.
How to handle the interface of an application is becoming increasingly challenging. A big benefit of MVC is that it can handle many different views for your application. There is actually no real processing in the view, whether this data is stored online or a list of employees, as a view, it is just a way to output data and allow users to manipulate it.
Model
The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects like EJBs and ColdFusion Components to handle the database. The data returned by the model is neutral, that is, the model is independent of the data format, and such a model can provide data for multiple views. Since the code applied to the model can be reused by multiple views only once, the code is reduced.
Controller
The controller accepts user input and calls models and views to fulfill user needs. So when clicking on the hyperlink in the web page and sending the HTML form, the controller itself does not output anything and does any processing. It simply receives the request and decides which model component to call to process the request, and then uses the determination of which view to display the data returned by the model processing.
Now we summarize the MVC processing process. First, the controller receives the user's request and decides which model should be called for processing. Then the model uses business logic to process the user's request and returns the data. Finally, the controller formats the data returned by the model with the corresponding view and presents it to the user through the presentation layer.
Why use MVC
Most web applications are created in procedural languages like ASP, PHP, or CFML. They mix data layer code like database query statements with presentation layer code like HTML.
Experienced developers will separate data from the presentation layer, but this is usually not easy to do, and it requires careful planning and continuous attempts. MVC is fundamentally mandatory to separate them. Although building an MVC application requires some extra work, the benefits it brings to us are unquestionable.
First, the most important thing is that multiple views share a model, as I mentioned, there are more and more ways to access your application now. One solution to this is to use MVC, whether your users want Flash interfaces or WAP interfaces; they can be processed with a model. Since you have separated data and business rules from presentation levels, you can maximize the reuse of your code.
Since the data returned by the model is not formatted, the same components can be used by different interfaces. For example, a lot of data may be represented in HTML, but they may also be represented in Macromedia Flash and WAP. The model also has functions for state management and data persistence processing. 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 separate 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, just change your model.
Once you implement the model correctly, no matter your data comes from the database or the LDAP server, the view will display them correctly. Since the three components of an application using MVC are opposite each other, changing one will not affect the other two, you can construct a well-loose coupling component based on this design idea.
For me, the controller also provides an advantage, which is that it can use the controller to connect different models and views to meet the user's needs, so that the controller can provide a powerful means for constructing the application. Given some reusable models and views, the controller can select the model to process according to the user's needs, and then select the view to display the processing results to the user.
Disadvantages of MVC
The disadvantage of MVC is that it is not clear that it is not easy to fully understand MVC. Using MVC requires careful planning, and because its internal principles are relatively complex, it takes some time to think.
You will have to spend considerable time thinking about how to apply MVC to your application. At the same time, since the model and view are strictly separated, this also creates some difficulties in debugging the application. Each component needs to be thoroughly tested before use. Once your components have been tested, you can reuse them without any scruples.
In my personal experience, since we split an application into three parts, using MVC also means you will have to manage more files than before, which is obvious. It seems like our workload has increased, but remember that the benefits it can bring us are not worth mentioning.
MVC is not suitable for small or even medium-sized applications, and spending a lot of time applying MVC to applications that are not very large will usually not be worth the effort.
MVC is a good way to create software
The MVC design pattern is a good way to create software. Some of the principles it advocates, such as separation of content and display, may be easier to understand. But if you are isolating the components of the model, view, and controller, you may need to rethink your application, especially the architecture aspect of the application. If you are willing to accept MVC and have the ability to cope with the additional work and complexity it brings, MVC will take your software to a new level in terms of robustness, code reuse and structure.