Model-View-controller (MVC) is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s. It has been widely used. Sun's J2EE platform design model has been recommended in recent years 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.
How MVC works
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.
View
A view is the interface on which the user sees and interacts with it. 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 have emerged, including Macromedia Flash and some identification languages such as XHTML, XML/XSL, and WML and Web services.
How to process the application interface becomes increasingly challenging. One major benefit of MVC is that it can process many different views for your 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.
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 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.
Controller
The Controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on a web page and send an HTML form, the controller does not output anything 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 data returned by the model for processing.
Now let's summarize the MVC processing process. First, the Controller receives user requests and decides which model should be called for processing. Then, the model uses the business logic to process user requests and return data, finally, the controller uses the corresponding view to format the data returned by the model and presents it to the user through the presentation layer.
Why use MVC?
Most Web applications are created using procedural languages such as ASP, PHP, and CFML. They mix data-Layer Code such as database query statements and presentation-Layer Code such as HTML. Experienced developers will separate the data from the presentation layer, but this is usually not easy to do. It requires careful planning and continuous attempts. MVC strictly separates them. Although constructing an MVC application requires some additional work, the benefits it brings to us are unquestionable.
First, the most important thing is that multiple views can share a model. As I mentioned, you need to access your application 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 Macromedia 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 opposite to 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 benefit 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 an application. 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.
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.
In my personal experience, since we split 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.
MVC is a good way to create software
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.