MVC design mode brings better software structure and code reuse

xiaoxiao2021-03-06  114

Model - View - Controller (MVC) is a software design pattern of Xerox Parc in the 1980s for programming language SMALLTALK-80, has been widely used. Recommended for Sun J2EE platform in recent years, and is popular with more and more developers using ColdFusion and PHP. Model - View - Controller mode is a useful toolbox, which has a lot of benefits, but there are also some shortcomings.

How to work MVC

MVC is a design pattern that is mandatory to separate the input, processing, and output of the application. Use the MVC application to be divided into three core components: model, view, controller. They have their own tasks.

view

The view is an interface that the user sees and interacts with it. For old-fashioned web applications, the view is an interface consisting of HTML elements. In the new web application, HTML still plays an important role in the view, but some new technologies have emerged, including Macromedia Flash and Like XHTML, XML / XSL, WML, etc. Identifier language and web services.

How to handle the interface of the application becomes more challenging. MVC a big benefit is that it can handle many different views for your application. In the view, there is no real handling, regardless of the online storage or an employee list, as a view, it is just as an output data and allows the user to manipulate.

model

The model represents enterprise data and business rules. In the three components of the MVC, the model has the most processing tasks. For example, it may handle the database with components like EJBS and ColdFusion Components. The data returned by the model is neutral, that is, the model is independent of the data format, so a model can provide data for multiple views. Since the code applied to the model can be reused by multiple views only, the repeatability of the code is reduced.

Controller

The controller accepts the user's input and calls models and views to complete the user's needs. So when you click the hyperlink and send an HTML form in the web page, the controller itself does not output anything and do anything. It just receives requests and decides which model component to deal with, and then use which view to display the data returned by the model processing.

Now we summarize the process of MVC, first control the controller to receive the user's request, and decide which model should be called to process, then model the business logic to process the user's request and return data, the last controller format the model to format the model. The data is presented to the user by means of a layer.

Why use MVC

Most of the web applications are created in procedural languages ​​like ASP, PHP, or CFML. They will mix the data layer code like the database query statement and a representation layer code like HTML. Experience is more experienced to separate data from the representation, but this is usually not easy to do, it requires careful plans and constant attempts. MVC is fundamentally mandatory to separate them. Although constructing a MVC application requires some additional work, it gives us the benefits of being unspected.

First, the most important point is that multiple views can share a model, as I mentioned, now you need to use more and more ways to access your app. In this regard, one of the solutions is to use MVC, regardless of your user wants the flash interface or WAP interface; you can handle them with a model. Since you have already separated the data and business rules from the representation, you can maximize your code.

Since the data returned by the model is not formatted, the same components can be used by different interfaces. For example, many data may be represented by HTML, but they may also be represented by Macromedia Flash and WAP. The model also has the function of state management and data persistence processing, for example, session-based shopping carts and e-commerce processes can be reused by the Flash website or wireless network. Because the model is self-contained and separated from the controller and view, it is easy to change the data layer and business rules for your application. If you want to transplant your database from MySQL to Oracle, or change your RDBMS source to LDAP, just change your model. Once you have a model correctly, the view will display correctly regardless of your data from the database or an LDAP server. Since three components that use MVC applications are mutually pertide, the change is that one of them does not affect other two, so it can construct a good pussy component based on this design idea.

For me, the controller also provides a benefit that can use the controller to connect different models and views to complete the user's needs so that the controller can provide powerful means for constructor. Given some reusable models and views, the controller can process according to the user's needs selection model, and then select the view to display the processing result to the user.

Disadvantages of MVC

The disadvantage of MVC is that it is not easy to understand that MVC is not easy because it has no clear definition. Using MVC requires a careful plan, because its internal principles are more complicated, it takes some time to think.

You will have to spend a considerable time to consider how to use MVC to your application, while the model and view must be strictly separated, which also gives the debugging application have come to a certain difficulty. Each component needs to be thoroughly tested before use. Once your components have been tested, you can use them without any scruples.

Based on my personal experience, since we divide an app into three components, use MVC simultaneously means that you will manage more than before, this is obvious. This seems that our workload is increased, but remember this is not worth mentioning than the benefits that it can bring.

MVC is not suitable for small or even medium-scale applications, which takes a lot of time to apply MVC to scale is not a big application usually.

MVC is a good way to create software

The MVC design model is a way to create software, some of its principles, like content and display mutual separation may be better understood. But if you want to isolate the model, view, and controller components, you may need to think about your application, especially the architecture of the application. If you are willing to accept MVC, and you have the ability to cope with the additional work and complexity it, MVC will make your software in a new step in terms of robust, code reuse, and structure.

转载请注明原文地址:https://www.9cbs.com/read-101757.html

New Post(0)