What is the MVC framework for you here

  • 2021-10-16 01:23:43
  • OfStack

MVC (Model View Controler) originally exists in Desktop program, M refers to data model, V refers to user interface, and C refers to controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different representations. For example, a batch of statistics can be represented by histogram and pie chart respectively. The purpose of C is to ensure the 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 in 810's for programming language Smalltalk-80, and has been widely used up to now. In recent years, it has been recommended as the design pattern of J2EE platform of Sun Company, and has been welcomed by more and more developers using ColdFusion and PHP. The Model-View-Controller pattern is a useful toolkit, which has many advantages, but also has some disadvantages.

How does MVC work

MVC is a design pattern that compels the separation of application input, processing, and output. Applications using MVC are divided into three core components: model, view and controller. They each handle their own tasks.

View

Views are interfaces that users see and interact with. For the old Web application, the view is an interface composed of HTML elements. In the new Web application, HTML still plays an important role in the view, but a number of new technologies have emerged, including Macromedia Flash, markup languages such as XHTML, XML/XSL, WML and Web services.

How to deal with the interface of applications becomes more and more challenging. The big benefit of MVC1 is that it can handle many different views for your application. No real processing takes place 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.

Model

Models represent enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it might use artifact objects like EJBs and ColdFusion Components to work with the database. The data returned by the model is neutral, which means that the model is independent of the data format, so that one model can provide data for multiple views. Because the code applied to the model can be reused by multiple views only once, code repetition is reduced.

Controller

The controller accepts the user's input and calls the model and view to fulfill the user's requirements. So when you click the hyperlink in the Web page and send the HTML form, the controller itself does nothing and does nothing. It simply receives the request and decides which model artifact to call to process the request, and then determines which view to use to display the data returned by the model process.

Now we summarize the processing process of MVC. 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 return data. Finally, the controller formats the data returned by the model with corresponding views 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, it requires careful planning and constant trial and error. MVC essentially forcibly separates them. Although building an MVC application requires a bit of extra work, the benefits it brings to us are unquestionable.

First of all, the most important point is that multiple views can share a model. As I mentioned, there are more and more ways to access your application. To this, one of the solutions is to use MVC, whether your users want Flash interface or WAP interface; They can be processed with one model. Since you have separated data and business rules 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, a lot of data may be represented by HTML, but they may also be represented by Macromedia, Flash and WAP. The model also has state management and data persistence processing capabilities. For example, session-based shopping carts and e-commerce processes can also be reused by Flash Web sites or wireless networking applications.

Because models are self-contained and separate from controllers and views, it is easy to change your application's data tier and business rules. If you want to migrate your database from MySQL to Oracle, or change your RDBMS-based data source to LDAP, just change your model.

1 Once you implement the model correctly, whether your data comes from a database or an LDAP server, the view will display them correctly. Because the three components of an application using MVC are opposite to each other, changing one of them will not affect the other two, so you can construct a good loosely coupled component according to this design idea.

For me, the controller also provides a benefit, that is, the controller can be used to connect different models and views to fulfill the user's needs, so that the controller can provide a powerful means for constructing applications. Given a set of reusable models and views, the controller can select models to process according to the user's needs, and then select views to display the processing results to the user.

Disadvantages of MVC

The disadvantage of MVC is that it is not clearly defined, so it is not easy to fully understand MVC. Using MVC requires careful planning, and because of its complex internal principles, it takes some time to think about it.

You will have to spend considerable time thinking about how to apply MVC to your application, and the strict separation of models and views makes debugging applications difficult. Each component needs to be thoroughly tested before use. 1 Once your components have been tested, you can reuse them without scruple.

From my personal experience, since we split an application into three parts, using MVC also means that you will manage more files than before, which is obvious. It seems that our workload has increased, but please remember that this is not worth mentioning compared with the benefits it can bring us.

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 usually does more harm than good.

MVC is a good way to create software

The MVC design pattern is a good way to create software, and the principles it advocates, such as the separation of content and presentation, 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 are able 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.


Related articles: