MVC (model view controller) originally exists in the desktop program. M is the data model, V is the user interface, and C is 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 forms. For example, a batch of statistics can be represented by a bar chart or a pie chart. The purpose of C 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 pattern invented by Xerox PARC for the programming language Smalltalk-80 in the 1980s, and has been widely used. In recent years, it has been recommended as the design pattern of J2EE platform of Sun company, and it is welcomed by more and more developers who use ColdFusion and PHP. The model view controller pattern is a useful toolbox that has many advantages but also some disadvantages.
How does MVC work
MVC is a design pattern that forces the separation of input, processing, and output of an application. Using MVC, applications are divided into three core components: model, view, and controller. They deal with their own tasks.
A view is an interface that users see and interact with. For the old web applications, view is the interface composed of HTML elements. In the new web applications, HTML still plays an important role in the view, but some new technologies have emerged in endlessly, including Macromedia Flash, some markup languages such as XHTML, XML / XSL, WML and web services
How to deal with the application interface is becoming more and more challenging. One of the big benefits of MVC is that it can handle many different views for your application. In fact, there is no real processing in the view. Whether the data is stored online or a list of employees, as a view, it is only a way to output data and allow users to manipulate it.
The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it might use component objects like EJBs and ColdFusion components to process a database. The data returned by the model is neutral, that is, the model is independent of 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 after being written once, the repeatability of the code is reduced.
The controller accepts user input and invokes models and views to fulfill user requirements. So when you click a hyperlink in a web page and send an HTML form, the controller itself doesn’t output anything or do anything about it. It just receives the request and decides which model component to call to process the request, and then determines which view to use to display the data returned by the model.
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 the data from the presentation layer, but this is usually not easy to do. It requires careful planning and constant trial and error. MVC fundamentally forces them apart. Although it takes some extra work to build an MVC application, there is no doubt that it will benefit us.
First of all, the most important thing is that multiple views can share a model, and as I mentioned, there are more and more ways to access your application. One of the solutions to this is to use MVC, whether your users want flash or WAP interfaces; a model can handle them. Since you’ve separated the data and business rules from the presentation layer, you can maximize the reuse of your code.
Since the data returned by the model is not formatted, the same component 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. For example, session based shopping cart and e-commerce process can also be reused by flash website or wireless networking application.
Because models are self-contained and separate from controllers and views, it’s easy to change the data layer and business rules of your application. If you want to change your data source from LDAP to MySQL, you can just change your data source from LDAP to MySQL.
Once you implement the model correctly, whether your data comes from a database or an LDAP server, the view will display it correctly. Because the three components of MVC application are opposite to each other, changing one of them will not affect the other two, so according to this design idea, you can construct good loosely coupled components.
For me, the controller also provides an advantage, that is, the controller can be used to connect different models and views to meet the needs of users, so that the controller can provide a powerful means for building applications. Given some reusable models and views, the controller can select the model to process according to the user’s requirements, 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 easy to fully understand MVC because it has no clear definition. Using MVC requires careful planning. Because of its complicated internal principles, 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, due to the strict separation of model and view, it is difficult to debug the application. Each component needs to be thoroughly tested before use. Once your components have been tested, you can reuse them without fear.
In my personal experience, since we have divided an application into three parts, using MVC also means that you will have to manage more files than before, which is obvious. It seems that our workload has increased, but please remember that it is not worth mentioning compared with the benefits it can bring us.
MVC is not suitable for small or even medium-sized applications. It is usually not worth the loss to spend a lot of time Applying MVC to applications of small or medium scale.
MVC is a good way to create software
MVC design pattern is a good way to create software. Some principles advocated by it, such as separation of content and display, may be better understood. But if you want to isolate the components of models, views, and controllers, you may need to rethink your application, especially the architectural aspects of your application. If you are willing to accept MVC and have the ability to cope with the extra work and complexity it brings, MVC will take your software to a new level in terms of robustness, code reuse and structure.