MVC Framework
The full name of MVC is the Model View controller, which is the abbreviation for Models-View-controller, a software design paradigm that began in 1982. Organize your code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.
Brief introduction
MVC begins in the desktop program, M refers to the business model, v refers to the user interface, C is the controller, the purpose of using MVC is to separate m and v implementation code, so that the same program can use different representations. For example, a batch of statistical data can be represented by histogram, pie chart. C exists to ensure the synchronization of M and V, and once M is changed, V should be updated synchronously.
Model-View-Controller (MVC) is a software design pattern invented by Xerox PARC in the the 1980s for programming language Smalltalk-80, which has been widely used. It was later recommended as a design model for Oracle's Sun Company's Java EE platform and was welcomed by a growing number of developers using ColdFusion and PHP. Model-View-controller mode is a useful toolbox that has many benefits, but also has some drawbacks.
MVC Programming Patterns
MVC is a pattern of creating WEB applications using MVC (Model View Controller-View-controller) Design:
- Model (models) represent the core of the application (such as a list of database records).
- View displays data (database records).
- The controller (Director) processes the input (writes to the database record).
The MVC pattern also provides complete control over HTML, CSS, and JavaScript.
Model (model) is the part of your application that handles application data logic.
Typically, model objects are responsible for accessing data in the database.
View (view) is the part of the application that handles the display of data.
Typically views are created from model data.
Controller (Controller) is the part of the application that handles user interaction.
Usually the controller is responsible for reading data from the view, controlling user input, and sending data to the model.
MVC layering helps you manage complex applications because you can focus on one aspect at a time. For example, you can focus on view design without relying on business logic. It also makes testing of applications easier.
The MVC hierarchy also simplifies group development. Different developers can develop views, controller logic, and business logic at the same time.
Application of MVC in Java Web application
The so-called MVC, namely Model-view-controller.
(1) Model layer: Model refers to the models section, generally in the application model layer includes the business processing layer and the data access layer. The data access layer is primarily a encapsulation of some operations on the database. The business processing layer is built using JavaBean, and JavaBean is primarily used to bridge data from the view layer and the data in the database. In addition to JavaBean, if you want to build a distributed application system, you can use EJB components to build a business logic layer.
(2) Controller layer: Controller refers to the control section, generally the view layer submitted by the request to set the corresponding servlet for the specific function of processing, where the processing of specific functions is generally written in the model of the business processing layer. A controller is typically just a mediator in a Web application.
(3) View layer: View refers to the Views section, this part of the content is presented to the user to actually interact, usually using JSP and HTML to build.
Framework Content
MVC refers to some kind of framework of the MVC pattern, which makes it mandatory to separate the input, processing, and output of the application. Using an MVC application is divided into three core parts: model, view, controller. Each of them handles their own tasks. The most typical MVC is the JSP + servlet + JavaBean pattern.
Model
Models represent enterprise data and business rules. Of the three parts of MVC, the model has the most processing tasks. For example, it may use a Component object such as EJBS and coldfusioncomponents to process the database, the data returned by the model is neutral, that is, the model is independent of 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, it reduces the repetition of the code.
View
A view is an interface that the user sees and interacts with. For older Web applications, views are an interface of HTML elements, and in modern Web applications, HTML still plays an important role in the view, but new technologies are emerging, including adobeflash and like Xhtml,xml/xsl, Some identity languages and webservices, such as WML.
The benefit of MVC is that it can handle many different views of the application. There is really no real processing happening in the view, whether the data is stored online or an employee list, as a view, it is simply a way to output data and allow the user to manipulate it.
Controller
The controller accepts the user's input and invokes the model and view to complete the user's needs, so when you click a hyperlink in a Web page and send an HTML form, the controller itself does not export anything and do any processing. It simply receives the request and decides which model component is called to process the request, and then determines which view to use to display the returned data.
Difference
The difference between a frame and a design pattern
Many programmers often confuse frame patterns with design patterns, thinking that MVC is a design pattern. In fact, they are completely different concepts.
The two concepts of frame and design pattern are always easy to be confused, but there are still differences between them. Frameworks are often code reuse, while design patterns are reuse, schemas are in between, part of code reuse, partial design reuse, and sometimes analysis can be reused. There are three levels of reuse in software production: internal reuse, an abstract block that can be used publicly in the same application, code reuse, the assembly of common modules into libraries or toolsets for use in multiple applications and domains, and the reuse of application frameworks that provide a common or off-the-shelf infrastructure for specialized areas For the highest level of reusability.
Although the framework is similar to the design pattern, it is fundamentally different. The design pattern is a description of the recurring problems in an environment and the solution to the problem, which is more abstract than the framework, the framework can be expressed in code, can be directly executed or reused, and only instances of the schema can be represented in code; Design patterns are smaller elements than frames. A framework often contains one or more design patterns, and the framework is always targeted at a particular application area, but the same pattern can be applied to a variety of applications. It can be said that the framework is software, while design patterns are software knowledge.
What are the framework modes?
MVC, MTV, MVP, CBD, ORM and so on;
What are the frameworks?
C + + language qt, MFC, Gtk,java language ssh,SSI, PHP language Smarty (MVC mode), Python language Django (MTV mode), etc.
What are the design patterns?
Factory mode, adapter mode, policy mode, etc.
In short: The framework is a great wisdom for the division of Software design, design pattern is a small skill, the specific problems proposed solutions to improve code reuse rate, reduce coupling.
Features advantages
1. Low coupling
The view layer is decoupled from the business layer, allowing changes to the view layer code without recompiling the model and controller code, as well as changes in an application's business process or business rules only need to change the MVC model layer. Because the model is decoupled from the controller and view, it is easy to change the data layer and business rules of the application.
The model is self-contained and detached from the controller and view, so it is easy to change the data layer and business rules of the application. If you migrate a database from MySQL to Oracle, or change an RDBMS-based data source to LDAP, you can simply change the model. Once the model is implemented correctly, the view will display them correctly, regardless of whether the data is from the database or the LDAP server. Since the three parts of an application using MVC are independent, changing one does not affect the other two, so a good loosely coupled component can be constructed based on this design idea.
2. high re-usability
As technology continues to evolve, more and more ways are needed to access applications. The MVC pattern allows you to access the same server-side code using a variety of different styles of view, because multiple views can share a model that includes any web (HTTP) browser or wireless browser (WAP), for example, a user can order a product from a computer or via a mobile phone. Although the order is not the same way, but the way to deal with the order of products is the same. Since the data returned by the model is not formatted, the same artifacts can be used by different interfaces. For example, many of the data may be represented in HTML, but it is also possible to use WAP to represent the commands required to change the implementation of the view layer without any changes to the control layer and the model layer. Since data and business rules have been separated from the presentation layer, it is possible to maximize the reuse of code. The model also has the capability of state management and data persistence processing, for example, session-based shopping carts and e-commerce processes can also be reused by flash sites or wireless networking applications. [11]
3. Low life cycle cost
MVC reduces the technical content of developing and maintaining user interfaces.
4 , Deployment Fast
Using the MVC pattern allows for a considerable reduction in development time, which allows programmers (Java developers) to focus on business logic, and interface programmers (HTML and JSP developers) focus on presentation.
5 , High maintainability
Separating the view layer and the business Logic layer also makes Web applications easier to maintain and modify.
6 , advantageous to the software engineering management
Because different layers have their own roles, each layer of different applications has some of the same characteristics, which facilitates engineering and tool-based management of program code. The controller also provides the benefit of using a controller to join different models and views to complete the user's needs, so that the controller can provide a powerful means of constructing the application. Given some reusable models and views, the controller can select the model to process based on the user's needs, and then select the view to display the processing results to the user.
Disadvantages
1 , without a clear definition
It's not easy to fully understand MVC. Using MVC requires careful planning, and because its internal principles are complex, it takes some time to think. At the same time, due to the strict separation of the model and view, this also brings some difficulties to debug the application. Each component needs to be thoroughly tested before it can be used.
2 , not suitable for small, medium-sized applications
It takes a lot of time to apply MVC to applications that are not large in size and often outweigh the costs.
3 , increase the complexity of the system structure and implementation
For a simple interface, strict adherence to MVC, which separates the model, view, and controller, increases the complexity of the structure and may result in excessive update operations and reduced operational efficiency.
4 , the overly tight connection between the view and the controller
Views and controllers are separated from each other, but are tightly connected parts, the view has no controller, its application is very limited, and vice versa, which hinders their independent reuse.
5 , view inefficient access to model data
Depending on the model operator interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also compromise operational performance.
6 , General advanced Interface tool or constructor does not support mode
The cost of transforming these tools to accommodate MVC needs and to build separate parts is high, which can make MVC difficult to use.
MVC design Pattern