Design
Model-View-Controller (MVC) is a software design pattern invented by Xerox PARC in the 80 's for programming language Smalltalk-80, which has been widely used so far. It has been recommended in recent years as a design model for Sun's Java-EE platform and is being welcomed by more and more developers using ColdFusion and PHP. Model-View-controller mode is a useful toolbox, it has a lot of benefits, but there are some drawbacks.
How MVC works
MVC is a design pattern that makes it mandatory to separate the input, processing, and output of an application. The MVC application is divided into three core components: models, views, controllers. Each of them handles its own task.
View
A view is an interface that users see and interact with. For older Web applications, views are an interface of HTML elements, and in new Web applications, HTML still plays an important role in view, but some new technologies are emerging, including Macromedia Flash and Elephant xhtml,xml/xsl , WML, and some other identity languages and Web services.
How to handle the interface of an application is becoming more and more challenging. One big advantage of MVC is that it can handle many different views for your application. There is actually no real processing happening in the view, whether the data is stored online or an employee list, which, as a view, is just a way of outputting data and allowing the user to manipulate it.
Model
The model represents enterprise data and business rules. In the three parts of MVC, the model has the most processing tasks. For example, it might use a Widget object such as EJBS and ColdFusion components to process the database. The data returned by the model is neutral, that is, the model is independent of the data format, such that a model can provide data for multiple views. Because the code applied to the model can be reused by multiple views only once, it reduces the duplication of the code.
Controller
The controller accepts input from the user and invokes the model and view to fulfill the user's needs. So when you click a hyperlink in a Web page and send an HTML form, the controller itself does not output anything or do anything with it. It simply receives the request and decides which model widget to call to process the request, and then uses the data that determines which view to use to display the model processing returned.
Now we summarize the process of MVC, first the controller to receive the user's request, and decide which model should be called to process, and then the model with business logic to process the user's request and return data, the last controller in the corresponding view format the model returned data, and presented to the user through the presentation layer.
Why to use MVC
Most Web applications are created using a procedural language such as asp,php, or CFML. They mix data-tier code like database query statements with presentation-layer code like HTML. The more experienced developers separate the data from the presentation layer, but this is often not easy to do, it requires careful planning and constant experimentation. MVC is fundamentally mandatory to separate them. Although it takes some extra work to construct an MVC application, the benefits it brings us are undoubtedly questionable.
First, the most important point is that multiple views can share a model, and as I mentioned, there are more and more ways to access your application. One solution to this is to use MVC, whether your users want the Flash interface or the WAP interface, and use a model to handle them. Since you have separated the data from the presentation layer, you can maximize the reuse of your code.
Because the data returned by the model is not formatted, the same artifacts can be used by different interfaces. For example, many data may be expressed in HTML, but they may also be expressed in Macromedia Flash and WAP. The model also has the function of state management and data persistence processing, for example, the conversation-based shopping cart and the e-business process can also be reused by flash websites or wireless networked applications.
Because the models are self-contained and separate from the controller and view, it is easy to change the data-tier and business rules of your application. If you want to migrate your database from MySQL to Oracle, or change your RDBMS data source to LDAP, just change your model. Once you have implemented the model correctly, regardless of whether your data comes from a database or an LDAP server, the view will display them correctly. Since the three parts of an application using MVC are antagonistic, changing one does not affect the other two, so you can construct a good loosely coupled component based on this design idea.
For me, the controller also provides the benefit of using a controller to join different models and views to fulfill the user's needs, so that the controller can provide a powerful tool for structuring the application. Given some reusable models and views, the controller can select the model to be processed according to the user's requirements, and then select the view to display the processing results to the user.
the drawbacks of MVC
The disadvantage of MVC is that it is not easy to fully understand MVC because it is not explicitly defined. Using MVC requires careful planning, because its internal principles are more complex, so it takes some time to think.
You will have to spend a considerable amount of time thinking about how to apply MVC to your application, and because of the strict separation of the model and the view, it is also difficult to debug the application. Each component needs to be thoroughly tested before it can be used. Once your components have been tested, you can reuse them without scruple.
Based on my personal experience, since we have divided an application into three parts, using MVC also means that you will be managing more files than before, which is obvious. It seems that our workload has increased, but remember that it is not worth mentioning the benefits it can bring us.
MVC is not suitable for small and even medium-size applications, and spending a lot of time applying MVC to applications that are not large in size will often outweigh the gains.
MVC is a great way to create software
MVC design pattern is a good way to create software, it advocates some principles, such as content and display separation may be better understood. But if you want to isolate the artifacts of models, views, and controllers, you may need to rethink your application, especially the architectural aspects of your application. If you accept MVC and have the ability to cope with the extra work and complexity it brings, MVC will make your software a new step in robustness, code reuse, and architecture.