MVC pattern
MVC pattern is . MVC application always by these three parts. Event (events) lead to changes in Controller or Model View, or both to change both. Models change as long as the Controller of data or property, all depend on View are automatically updated. Similarly, as long as the Controller changed the View, View of the Model from the potential to obtain data to refresh themselves. MVC pattern was first made smalltalk language research group, applied to the user interaction application. smalltalk java language language and there are many similarities, are object-oriented language, it is natural SUN in the petstore (pet shops) in case the application to MVC model as the recommended framework for developing Web application model. MVC pattern is an architectural pattern, in fact, other models need to be done in collaboration. In the J2EE pattern catalog, usually achieved by service to worker mode, and service to worker mode controller mode can be centralized, dispatch mode and Page Helper mode composition. Struts implements the MVC but only the two parts View and Controller, Model part of the need to develop themselves to achieve, Struts Action class provides the abstract so that developers can apply Model Struts framework.
MVC pattern is a complex structure model, its implementation has become very complex. However, we have summarized a lot of reliable design patterns, a variety of design patterns together, so that implementation of MVC pattern becomes relatively easy. Views can be seen as a tree, obviously with the Composite Pattern to achieve. Views and Models of the relationship between the expression with the Observer Pattern. Views Controller control the display, you can use Strategy Pattern implementation. Model is usually a mediator, can be used to achieve the Mediator Pattern.
Now let's look at three parts of MVC architecture in J2EE at what position, which will help us understand the MVC pattern. MVC and J2EE architecture mapping is: View in the Web Tier or a Client Tier, usually a JSP / Servlet, the page display section. Controller also in the Web Tier, usually Servlet to achieve, that is, the logical part of the realization of the page display. Model in the Middle Tier, usually the javaBean or EJB server implementation, the business logic implementation.
one, MVC design
MVC in English or Model-View-Controller, an application that is input, process, output process in accordance with the Model, View, Controller isolated manner, such a application is divided into three layers - the model layer, view layer, control layer.
view (View) on behalf of the user interface for Web applications, can be summarized as HTML interface, but has the potential to XHTML, XML, and Applet. With the application of the complexity and scale of the interface processing has become challenging. An application may have many different views, MVC design pattern for the view only view the processing of data acquisition and processing, and the user's request, but not in the view on the handling of business processes. The treatment given to model business processes (Model) treatment. For example, a view only accept orders from the model data and displayed to the user, and the user interface of the input data and passes the request to control and model.
model (Model): is the business process / status of the processing and business rules. Processing of business process layer is on the other black box, accept the view of the requested data model, and returns the final processing results. The design of business models can be said that the main core of MVC. Popular EJB model is a typical application example of application of technology from the perspective of the model has a further division in order to make full use of existing components, but it can not serve as a framework for application design model. It just tells you the design according to this model can take advantage of some of the technology components, thereby reducing the technical difficulties. For a developer, can focus on business model design. MVC design pattern tells us that the application of the model extracted according to certain rules, the level of extraction is very important, which is also the developer to determine whether the basis for good design. Abstract and concrete can not be separated too far, nor too close. MVC does not provide the model design and management should be organized only tell you that these models for the reconstruction of the model and improve reusability. We can do an analogy with object-oriented programming, MVC defines a top-class sub-class you tell it can only do this, but can not limit you can do these. This point is very important to program developers.
business model there is a very important model that is data model. Mainly refers to the entity object data model, data retention (persistence). Example, an order will be saved to the database, to obtain orders from the database. This model can be singled out, all the operation of the database is only limited to the model.
Control (Controller) can be interpreted as a request from the user receives will be matched with model and view together to accomplish the user's request. The role of division of control layer is also very obvious, it clearly tells you that it is a distribution, select what kind of model, choose what kind of view, what kind of users can complete the request. Control layer does not do any data processing. For example, the user clicks on a link, the control layer to accept the request, does not deal with business information, only the user's information to the model, to tell what model to select the view to meet the requirements to return to the user. Thus, a model may correspond to multiple views, one view may correspond to multiple models.
model, the separation of view and the controller, so a model can have multiple display views. If the user through a view controller to change the model data, all other views that depend on these data should reflect these changes. Therefore, regardless of what happens when the data changes, the controller will inform all of the view will change, causing display updates. This is actually a model of change - communication mechanism. Model, view, the relationship between the controller and their main function, as shown in Figure 1.
Second, MVC design pattern implementations
ASP.NET provides a good model to achieve this classic design a similar environment. ASPX pages developer to develop through the user interface to implement the view; controller function in the logic function code (. Cs) to achieve; model is generally consistent with the operational part of the application system. Implement this design in ASP.NET to provide a multi-layer system, the structure than the classic ASP systems to achieve significant advantages. The user display (the view) from the action (controller) in the isolated, improved code reuse. The data (model) from its operation of the action (controller) allows you to separate design a system independent of the background to store data. MVC structure to nature, it is a solution of the coupling system problems.
2.1
view model view is that it provides the user interface. Page contains a single display using multiple user parts, complex Web pages can display content from multiple data sources and web staff, artists can participate in these Web pages alone, the development and maintenance.
in ASP.NET, the view is very simple to achieve. WINDOWS interface can be as developed as the integrated development environment directly in the control by dragging the page to complete the development of this. Described in this article are used for each page view in the form of the compound: a page by multiple sub-view (the user part) composition; child view can be a simple HTML controls, server controls nested structure or more controls Web custom made control. Page is defined by the template, the template defines the layout of the page, the user part of the label and number, the user specify a template, the platform automatically create pages based on this information. Templates for static content, such as site navigation on the page, menu, friendly links to the content of the default template configuration; template for dynamic content (mainly business content), because of the different user's request can only be tied with the late set, and for users, the user to filter the display components. Components used by the user group composed of the template configuration page, it enhances the reusability and the prototype of the site's layout.
view of part of the general process flow is as follows: First, the page template defines the layout of the page; page configuration file defines the specific content of the view label (user parts); then, from page layout and load the class initialization strategy page; each user according to its own configuration component initialize, load the validator and set the parameters, and the event delegate and so on; users to submit, through the presentation layer validation, the user data is automatically submitted to the parts of the Business entities that model.
This part defines the base class WEB page PageBase; page layout policy class PageLayout, complete page layout, the user part is used to load the page; user component parts that the user base UserControlBase framework for the dynamic increase load test components and parts to achieve a personalized user. In order to achieve the flexibility of WEB application, view part of a number of configuration files are also used for example: set a template configuration file, page layout, path configuration, authentication configuration.
2.2 controller
order to control and coordinate across each user request processing, the control mechanism should be a centralized approach to management. Therefore, in order to achieve the purpose of the introduction of centralized management control. Centralized control application receives requests from the client (typically a user running the browser), decided to implement what business logic function, then the next step will have the responsibility of the user interface assigned to an appropriate view component.
control and use the controller to provide a centralized entry point to process the request, which is responsible for receiving, intercept and process user requests; and entrusted to the distributor class of the request, based on the current status and business operations and decided to customers rendered view. In this part, defines HttpReqDispatcher (distributor type), HttpCapture (request capture of class), Controller (Controller Class), etc., they complement each other to complete the controller functions. Captured by class capture HTTP requests and forwards the request to the controller class. The system controller class to handle all requests in the initial entry point. Controller to complete the necessary treatment to those who request entrusted to the distribution of class; distributor responsible for distribution of class management and navigation view, it manages to choose which view to the user, and control of resources available to distribute. Were used in this part of the distributor, strategy, factory method, adapters, and other design patterns.
order to request the user to capture those classes automatically capture and process the request, ASP.NET provides a low-level request / response API, allows developers to use. NET Framework classes to service incoming HTTP requests. Therefore, we must create the interface and implementation support System.Web.IHTTPHandler ProcessRequest () method of the class that: a request to capture those categories, and in the web.config of Add Class section. ASP.NET HTTP every received request was eventually passed by the implementation IHTTPHandler specific instance of the class to deal with. IHttpHandler instance IHttpHandlerFactory provided URL request handling the actual resolution of the structure. HTTP handlers and factories in the ASP.NET web.config configuration file as part of statement. ASP.NET configuration section defines a in which you can add and remove handlers and factories. Subdirectories inherit HttpHandlerFactory and HttpHandler settings. HTTP handlers and factories are the main ASP.NET page framework. Plant will be allocated to each request a handler, which handles the request. For example, in the global machine.config file, ASP.NET will ASPx all requests for files map to HttpCapture categories:
...
...
< / httphandlers>
2.3 model
MVC model system can be divided into two categories from the conceptual system and change the internal state of the system state action. Model is all your business logic code fragment lies. This article provides a business entity object model and business process object: the object of all business processes are derived from the ProcessBase class subclass. Business process object encapsulates the specific processing logic, call the business logic model, and the response submitted to the appropriate View component to produce the response. Business entity object attributes can be described by defining the client form data. All business entity objects EntityBase subclass object, business process object can be directly read and write it, and no longer need and request, response object data exchange. Achieved through business entity object model of the interaction between the view and support. Achieved when the This allows the reuse of business logic. Because each application specific business is different, this is no longer listed in the specific code examples.
III, MVC design pattern
by the expansion of the MVC pattern in ASP.NET prepared, with very good scalability. It can easily achieve the following functions:
① implement a model of multiple views;
② using multiple controllers;
③ When the model changes, all views will be auto-refresh;
④ all controllers will work independently of each other.
This is the advantage of MVC pattern, just a little in the previous procedure to modify or add new classes, you can easily increase the number of program features. Previously developed can be reused in many classes, but do not need to change the program structure, various independent of each other, to facilitate community development and improve development efficiency. How to implement a model discussed below, two views, and a controller program. Model classes and view class which do not need to change, and in front of exactly the same, this is the benefits of object-oriented programming. For the controller class, simply add another view, associated with the model can occur. The model view controller diagram between the three models shown in Figure 2.
can also achieve other forms of MVC for example: a model, two views and two controllers. It can be seen from above, through the application MVC pattern implementation is extremely good scalability is ASP.NET the future direction of object-oriented programming.
IV, MVC
most of the advantages of using process languages such as ASP, PHP developed Web applications, the initial development of the mixed layer is the data template programming. For example, send the request directly to the database and displayed using HTML, development speed is often faster, but because of the separation of the data page is not very direct, it is difficult to reflect the way the business model or the model of reuse. Flexibility in product design efforts in small, difficult to change to meet user demand. MVC requires the application layer, although it takes extra work, but the clear structure of products, product application through the model can be better reflected.
First, and most importantly, there should be multiple views corresponding to the ability of a model. In the current rapid changes in user requirements, there may be multiple ways to access applications. For example, the order model may be the order of the system, but also online orders, or orders for other systems, but the treatment is the same order, that order processing is the same. At MVC design pattern, an order models and multiple views to solve the problem. This reduces code replication, reducing the amount of code maintenance, once the model changes, but also easy to maintain. Second, the model of the data returned without any display format, so these models can be directly applied to the interface to use.
again, because an application is separated into three layers, so sometimes you can change them to meet the application layer of the change. Business processes of an application or business rules change just to change the model MVC layer.
control layer concept is also very effective, because it put the different models and different views together to complete various requests, therefore, the control layer can be said to include the concept of user requests permission.
Finally, it is also beneficial to software engineering management. Perform their duties due to the different layers, each layer has different applications some of the same features will help through the engineering, production management tools of program code.
five, MVC lack
MVC deficiencies reflected in the following areas:
(1) increases the system architecture and implementation complexity. For the simple interface, strictly follow the MVC, the model, view and controller separation, will increase the complexity of the structure and may have too many updates and lower operating efficiency.
(2) view and the controller connection between too closely. View and the controller are separate, but closely linked components do, do not view the existence of the controller, its application is very limited, and vice versa, thus impeding their independence reuse.
(3) view of the low efficiency of model data access. User interface based on the different models, the view may need to call to get sufficient display data. Does not change data on the frequency of unnecessary visits will also be harm operating performance.
(4) At the moment, the interface tools or advanced construction does not support the MVC pattern. Adaptation of these tools to meet the needs and the establishment of MVC separation of the cost of components is very high, resulting in difficulties in using the MVC.
MVC pattern is . MVC application always by these three parts. Event (events) lead to changes in Controller or Model View, or both to change both. Models change as long as the Controller of data or property, all depend on View are automatically updated. Similarly, as long as the Controller changed the View, View of the Model from the potential to obtain data to refresh themselves. MVC pattern was first made smalltalk language research group, applied to the user interaction application. smalltalk java language language and there are many similarities, are object-oriented language, it is natural SUN in the petstore (pet shops) in case the application to MVC model as the recommended framework for developing Web application model. MVC pattern is an architectural pattern, in fact, other models need to be done in collaboration. In the J2EE pattern catalog, usually achieved by service to worker mode, and service to worker mode controller mode can be centralized, dispatch mode and Page Helper mode composition. Struts implements the MVC but only the two parts View and Controller, Model part of the need to develop themselves to achieve, Struts Action class provides the abstract so that developers can apply Model Struts framework.
MVC pattern is a complex structure model, its implementation has become very complex. However, we have summarized a lot of reliable design patterns, a variety of design patterns together, so that implementation of MVC pattern becomes relatively easy. Views can be seen as a tree, obviously with the Composite Pattern to achieve. Views and Models of the relationship between the expression with the Observer Pattern. Views Controller control the display, you can use Strategy Pattern implementation. Model is usually a mediator, can be used to achieve the Mediator Pattern.
Now let's look at three parts of MVC architecture in J2EE at what position, which will help us understand the MVC pattern. MVC and J2EE architecture mapping is: View in the Web Tier or a Client Tier, usually a JSP / Servlet, the page display section. Controller also in the Web Tier, usually Servlet to achieve, that is, the logical part of the realization of the page display. Model in the Middle Tier, usually the javaBean or EJB server implementation, the business logic implementation.
one, MVC design
MVC in English or Model-View-Controller, an application that is input, process, output process in accordance with the Model, View, Controller isolated manner, such a application is divided into three layers - the model layer, view layer, control layer.
view (View) on behalf of the user interface for Web applications, can be summarized as HTML interface, but has the potential to XHTML, XML, and Applet. With the application of the complexity and scale of the interface processing has become challenging. An application may have many different views, MVC design pattern for the view only view the processing of data acquisition and processing, and the user's request, but not in the view on the handling of business processes. The treatment given to model business processes (Model) treatment. For example, a view only accept orders from the model data and displayed to the user, and the user interface of the input data and passes the request to control and model.
model (Model): is the business process / status of the processing and business rules. Processing of business process layer is on the other black box, accept the view of the requested data model, and returns the final processing results. The design of business models can be said that the main core of MVC. Popular EJB model is a typical application example of application of technology from the perspective of the model has a further division in order to make full use of existing components, but it can not serve as a framework for application design model. It just tells you the design according to this model can take advantage of some of the technology components, thereby reducing the technical difficulties. For a developer, can focus on business model design. MVC design pattern tells us that the application of the model extracted according to certain rules, the level of extraction is very important, which is also the developer to determine whether the basis for good design. Abstract and concrete can not be separated too far, nor too close. MVC does not provide the model design and management should be organized only tell you that these models for the reconstruction of the model and improve reusability. We can do an analogy with object-oriented programming, MVC defines a top-class sub-class you tell it can only do this, but can not limit you can do these. This point is very important to program developers.
business model there is a very important model that is data model. Mainly refers to the entity object data model, data retention (persistence). Example, an order will be saved to the database, to obtain orders from the database. This model can be singled out, all the operation of the database is only limited to the model.
Control (Controller) can be interpreted as a request from the user receives will be matched with model and view together to accomplish the user's request. The role of division of control layer is also very obvious, it clearly tells you that it is a distribution, select what kind of model, choose what kind of view, what kind of users can complete the request. Control layer does not do any data processing. For example, the user clicks on a link, the control layer to accept the request, does not deal with business information, only the user's information to the model, to tell what model to select the view to meet the requirements to return to the user. Thus, a model may correspond to multiple views, one view may correspond to multiple models.
model, the separation of view and the controller, so a model can have multiple display views. If the user through a view controller to change the model data, all other views that depend on these data should reflect these changes. Therefore, regardless of what happens when the data changes, the controller will inform all of the view will change, causing display updates. This is actually a model of change - communication mechanism. Model, view, the relationship between the controller and their main function, as shown in Figure 1.
Second, MVC design pattern implementations
ASP.NET provides a good model to achieve this classic design a similar environment. ASPX pages developer to develop through the user interface to implement the view; controller function in the logic function code (. Cs) to achieve; model is generally consistent with the operational part of the application system. Implement this design in ASP.NET to provide a multi-layer system, the structure than the classic ASP systems to achieve significant advantages. The user display (the view) from the action (controller) in the isolated, improved code reuse. The data (model) from its operation of the action (controller) allows you to separate design a system independent of the background to store data. MVC structure to nature, it is a solution of the coupling system problems.
2.1
view model view is that it provides the user interface. Page contains a single display using multiple user parts, complex Web pages can display content from multiple data sources and web staff, artists can participate in these Web pages alone, the development and maintenance.
in ASP.NET, the view is very simple to achieve. WINDOWS interface can be as developed as the integrated development environment directly in the control by dragging the page to complete the development of this. Described in this article are used for each page view in the form of the compound: a page by multiple sub-view (the user part) composition; child view can be a simple HTML controls, server controls nested structure or more controls Web custom made control. Page is defined by the template, the template defines the layout of the page, the user part of the label and number, the user specify a template, the platform automatically create pages based on this information. Templates for static content, such as site navigation on the page, menu, friendly links to the content of the default template configuration; template for dynamic content (mainly business content), because of the different user's request can only be tied with the late set, and for users, the user to filter the display components. Components used by the user group composed of the template configuration page, it enhances the reusability and the prototype of the site's layout.
view of part of the general process flow is as follows: First, the page template defines the layout of the page; page configuration file defines the specific content of the view label (user parts); then, from page layout and load the class initialization strategy page; each user according to its own configuration component initialize, load the validator and set the parameters, and the event delegate and so on; users to submit, through the presentation layer validation, the user data is automatically submitted to the parts of the Business entities that model.
This part defines the base class WEB page PageBase; page layout policy class PageLayout, complete page layout, the user part is used to load the page; user component parts that the user base UserControlBase framework for the dynamic increase load test components and parts to achieve a personalized user. In order to achieve the flexibility of WEB application, view part of a number of configuration files are also used for example: set a template configuration file, page layout, path configuration, authentication configuration.
2.2 controller
order to control and coordinate across each user request processing, the control mechanism should be a centralized approach to management. Therefore, in order to achieve the purpose of the introduction of centralized management control. Centralized control application receives requests from the client (typically a user running the browser), decided to implement what business logic function, then the next step will have the responsibility of the user interface assigned to an appropriate view component.
control and use the controller to provide a centralized entry point to process the request, which is responsible for receiving, intercept and process user requests; and entrusted to the distributor class of the request, based on the current status and business operations and decided to customers rendered view. In this part, defines HttpReqDispatcher (distributor type), HttpCapture (request capture of class), Controller (Controller Class), etc., they complement each other to complete the controller functions. Captured by class capture HTTP requests and forwards the request to the controller class. The system controller class to handle all requests in the initial entry point. Controller to complete the necessary treatment to those who request entrusted to the distribution of class; distributor responsible for distribution of class management and navigation view, it manages to choose which view to the user, and control of resources available to distribute. Were used in this part of the distributor, strategy, factory method, adapters, and other design patterns.
order to request the user to capture those classes automatically capture and process the request, ASP.NET provides a low-level request / response API, allows developers to use. NET Framework classes to service incoming HTTP requests. Therefore, we must create the interface and implementation support System.Web.IHTTPHandler ProcessRequest () method of the class that: a request to capture those categories, and in the web.config of Add Class section. ASP.NET HTTP every received request was eventually passed by the implementation IHTTPHandler specific instance of the class to deal with. IHttpHandler instance IHttpHandlerFactory provided URL request handling the actual resolution of the structure. HTTP handlers and factories in the ASP.NET web.config configuration file as part of statement. ASP.NET configuration section defines a in which you can add and remove handlers and factories. Subdirectories inherit HttpHandlerFactory and HttpHandler settings. HTTP handlers and factories are the main ASP.NET page framework. Plant will be allocated to each request a handler, which handles the request. For example, in the global machine.config file, ASP.NET will ASPx all requests for files map to HttpCapture categories:
...
...
< / httphandlers>
2.3 model
MVC model system can be divided into two categories from the conceptual system and change the internal state of the system state action. Model is all your business logic code fragment lies. This article provides a business entity object model and business process object: the object of all business processes are derived from the ProcessBase class subclass. Business process object encapsulates the specific processing logic, call the business logic model, and the response submitted to the appropriate View component to produce the response. Business entity object attributes can be described by defining the client form data. All business entity objects EntityBase subclass object, business process object can be directly read and write it, and no longer need and request, response object data exchange. Achieved through business entity object model of the interaction between the view and support. Achieved when the This allows the reuse of business logic. Because each application specific business is different, this is no longer listed in the specific code examples.
III, MVC design pattern
by the expansion of the MVC pattern in ASP.NET prepared, with very good scalability. It can easily achieve the following functions:
① implement a model of multiple views;
② using multiple controllers;
③ When the model changes, all views will be auto-refresh;
④ all controllers will work independently of each other.
This is the advantage of MVC pattern, just a little in the previous procedure to modify or add new classes, you can easily increase the number of program features. Previously developed can be reused in many classes, but do not need to change the program structure, various independent of each other, to facilitate community development and improve development efficiency. How to implement a model discussed below, two views, and a controller program. Model classes and view class which do not need to change, and in front of exactly the same, this is the benefits of object-oriented programming. For the controller class, simply add another view, associated with the model can occur. The model view controller diagram between the three models shown in Figure 2.
can also achieve other forms of MVC for example: a model, two views and two controllers. It can be seen from above, through the application MVC pattern implementation is extremely good scalability is ASP.NET the future direction of object-oriented programming.
IV, MVC
most of the advantages of using process languages such as ASP, PHP developed Web applications, the initial development of the mixed layer is the data template programming. For example, send the request directly to the database and displayed using HTML, development speed is often faster, but because of the separation of the data page is not very direct, it is difficult to reflect the way the business model or the model of reuse. Flexibility in product design efforts in small, difficult to change to meet user demand. MVC requires the application layer, although it takes extra work, but the clear structure of products, product application through the model can be better reflected.
First, and most importantly, there should be multiple views corresponding to the ability of a model. In the current rapid changes in user requirements, there may be multiple ways to access applications. For example, the order model may be the order of the system, but also online orders, or orders for other systems, but the treatment is the same order, that order processing is the same. At MVC design pattern, an order models and multiple views to solve the problem. This reduces code replication, reducing the amount of code maintenance, once the model changes, but also easy to maintain. Second, the model of the data returned without any display format, so these models can be directly applied to the interface to use.
again, because an application is separated into three layers, so sometimes you can change them to meet the application layer of the change. Business processes of an application or business rules change just to change the model MVC layer.
control layer concept is also very effective, because it put the different models and different views together to complete various requests, therefore, the control layer can be said to include the concept of user requests permission.
Finally, it is also beneficial to software engineering management. Perform their duties due to the different layers, each layer has different applications some of the same features will help through the engineering, production management tools of program code.
five, MVC lack
MVC deficiencies reflected in the following areas:
(1) increases the system architecture and implementation complexity. For the simple interface, strictly follow the MVC, the model, view and controller separation, will increase the complexity of the structure and may have too many updates and lower operating efficiency.
(2) view and the controller connection between too closely. View and the controller are separate, but closely linked components do, do not view the existence of the controller, its application is very limited, and vice versa, thus impeding their independence reuse.
(3) view of the low efficiency of model data access. User interface based on the different models, the view may need to call to get sufficient display data. Does not change data on the frequency of unnecessary visits will also be harm operating performance.
(4) At the moment, the interface tools or advanced construction does not support the MVC pattern. Adaptation of these tools to meet the needs and the establishment of MVC separation of the cost of components is very high, resulting in difficulties in using the MVC.