MVC mode and application-architecture mode in Web Systems

Source: Internet
Author: User

The object-oriented design model is a summary of experience, and the MVC idea was originally used to build a user interface. This article mainly discusses how to use the design pattern and MVC Architecture in the new web application field. This article first introduces the concept and features of the design pattern, as well as the design ideology of the MVC Architecture, and analyzes several main pattern contained in the MVC Architecture. Then, according to the characteristics of the Web application system, some design ideas are proposed on how to apply the model and MVC Architecture.

1. Introduction
1.1 Design Model

The emergence and Application of object-oriented technology greatly improves the reusability and quality of software. Object-Oriented Programming is simpler and more efficient than the previous programming modes, but object-oriented design methods are much more complex and skillful than previous design methods, A good design should be targeted to both the problem and the future problems and requirements should be fully considered. In the past 10 years, people have created some good solutions for some problems in the research and exploration of object-oriented technology and practical applications, that is, the so-called object-oriented design model. One of the purposes of object-oriented technology is to improve the reusability of software, while the reuse of design patterns and solutions reflects the significance and essence of reuse at a deeper level.
There are many definitions of design patterns, most of which are referenced by Christopher Alexander's design patterns: Each design pattern is a three-way rule, it expresses a context, a problem, and a solution. The design pattern generally has the following basic elements: Pattern name, question, purpose, solution, effect, sample code and related design pattern.
There are several types of design patterns. They can be divided into three types based on their purposes: creational, structural, and behavior. The creation mode is mainly used to create objects. The structure mode is mainly a combination of processing classes or objects, and the behavior mode is mainly used to describe how classes or objects interact and how to assign responsibilities. The design patterns can also be divided into class patterns and object patterns according to the scope. Class patterns process the relationship between classes and subclasses. These relationships are established by inheritance and determined at the Compilation Time, is static. The object mode is used to process the relationships between objects. These relationships change and are more dynamic at runtime.

Features of the pattern: It is obtained through experience and written in a structured format, avoiding the same problem and re-designing. It exists in different abstract layers and is constantly improving, it is a reusable artificial product that allows the design to interact with the best practice to be combined to solve larger problems.

1.2 MVC Architecture
MVC was initially used in Smalltalk-80 to build user interfaces. M represents the model, V represents the view, and C represents the controller.
The purpose of MVC is to increase the code reuse rate and reduce the coupling between data expression, data description, and application operations. It also greatly improves Software maintainability, maintainability, scalability, flexibility, and encapsulation.
An event-driven user interface is an organizational structure for a single user's applications. Developers use an interface tool to draw a user interface, and then write code to execute corresponding actions based on user input. Many interactive development environments encourage this, because it emphasizes that the interface is available first and then functional again. Some software design pattern strategies are like this, and then the fixed code is often integrated into the final system. As a result, the program organizes the user interface elements and user actions on those interface elements, data storage, application functions, and code used to display them in disorder. In a single user's system, the code structure can be like this, because the system requirements will not change frequently. But it is not suitable for a large system, such as a large web system or an e-commerce system.
The distributed system design can be improved by separating the data mode from various data that can be accessed and controlled. The MVC design pattern consists of three parts. A model is an application object without a user interface. The view shows the display on the screen and the data flowing to the user. The controller defines the response method of the user interface to the user input, and converts the user's actions into operations for the model. The model updates View data to reflect data changes.
Relationship

Understanding of the MVC Relationship Diagram

Figure 2 MVC division of labor and collaboration


2. Design Pattern in MVC
A system based on MVC contains many design patterns, but the three most closely related models are observer, composite, and strategy.
2.1 observer Mode
MVC separates model and view by order/notification. Make sure that the view displays the content and status of the model correctly. Once the content of the model changes, there must be a mechanism for the model to notify the relevant view, so that the relevant view can refresh the data at the appropriate time. This design can also solve more general problems and separate objects, so that changes to one object can affect other objects, and this object does not know the details of those affected objects. This is the design mode described as observer.

Mode type: The Observer mode is the object mode and the behavior mode.
Mode purpose: Define the one-to-many dependency between objects. When the value or status of an object changes, all objects that are dependent on it are notified and updated automatically. A data may be displayed in multiple ways, and may be displayed in different ways at the same time (2 ). When data is changed in one way, other displays should be able to immediately know the data changes and make corresponding adjustments.
Mode structure:

Figure 3. Structure of the observer Mode

Effect:
1. Abstract coupling. The target object only knows that it has some observers. Each Observer conforms to the simple interface of the abstract observer class and does not know which class they belong. This minimizes and abstracts the coupling between the target and the observer.
2. Supports broadcast communication. You do not need to specify the observer to send the notification to the target. The observer determines how to handle the notification.
3. Possible unexpected updates. Update logic should be processed to avoid updating errors.

2.2 composite mode
An important feature of MVC is that view can be nested. Nested composite views can be used where any view is available and can be used to manage nested views. This idea reflects the design that treats the combined view and its components equally. This design concept is described as a composite design pattern in the Object-Oriented field.
Mode type: the composite mode is an object mode and also a structural mode.
Mode objective: to combine objects into a tree structure to represent a "part-whole" hierarchy. Composite makes the use of composite objects consistent with that of a single object.
Mode structure:

Figure 4. Structure of composite mode


Effect:
1. defines a class hierarchy containing simple objects and composite objects. Simple objects can be combined into complex objects, and combined objects can be combined. In this way, the combined object can be used wherever simple objects are used in client code.
2. Simplified client code. The client does not need to know whether an object is a simple object or a composite object. These objects can be used in a consistent manner.
3. It is easier to add new types of components. New components can be easily added to existing composite objects without changing the client code.

2.3 Strategy Mode
Another important feature of MVC is that you can change the response mode of a view to user input without changing the view. This is very important for a system that often requires a change response logic. MVC encapsulates the response logic in the controller. There is a class hierarchy of the controller, which allows you to easily change the original controller and create a new controller. View uses instances of the controller subclass to implement a specific response policy. To implement different response policies, you only need to replace them with different types of controller instances. You can also change the view's Response Policy to user input by changing the view's controller at runtime. This view-controller relationship is an example of the design pattern described as strategy.
Mode type: the Strategy Mode is an object mode and also a behavior mode.
Pattern objective: to define a series of algorithms and encapsulate them so that they can be replaced with each other so that the algorithms can change independently from the clients that use them.
Mode structure:

Figure 5. Structure of the Strategy Mode

Effect:
1. The strategy class hierarchy defines reusable algorithms or actions for context.
2. Replace the inherited method. If you inherit the context directly and give different behaviors, the behavior will be added to the context to mix the implementation of the algorithm with the context, making it difficult to understand, maintain, and expand the context, in addition, algorithms cannot be dynamically changed. The algorithm is encapsulated in an independent strategy class, which makes the algorithm independent of context changes and easy to switch and expand.
3. Different implementations of the same behavior can be provided.
4. The client must understand the differences between strategy.
5. communication overhead between context and strategy.
6. added the number of objects.

3. Application of MVC in Web Systems
Currently, some web-based distributed systems, such as B2B e-commerce systems, are suitable for adopting the MVC Architecture.
Through analysis, an application object can be divided into three types from a high-level perspective. One is the objects that are responsible for display. The other is the objects that contain business rules and data. The other is the receiving of requests and controlling the commercial objects to complete the requests. The display of these applications often needs to be changed, such as the webpage style, color, content to be displayed, and content display method. Business Rules and data are relatively stable. Therefore, the view of the displayed object needs to change frequently, indicating that the object model of business rules and data is relatively stable, while the Controller that represents control is the most stable.
Generally, after a system is released, the view object is managed by the artist, HTML/jsp designer, or system administrator. The Controller object is developed and implemented by application developers. The commercial rule object and commercial data object are jointly implemented by developers, domain experts, and database administrators. The display logic is controlled at the web layer or client. It can be servlet or JSP, which dynamically generates HTML. Generally, JSP is better than servlet. JSP better separates code from HTML, which facilitates the separation of page designers and code developers and improves efficiency. At the same time, jsp can complete all the functions completed by Servlet. In fact, JSP is eventually converted into a servlet. Control-related objects exist at every level of the system to coordinate cross-layer actions. Objects that contain business rules and data exist in the EJB layer (EJB-centric mode) or web layer (Web-centric mode ).

3.1 application of View in Web System
View represents the display of the system, which exists completely on the web layer. Generally, it consists of JSP, Java Bean, and custom tag. JSP can dynamically generate webpage content. custom tag makes it easier to use Java Bean, and it can encapsulate the display logic, which is more conducive to modularization and reuse. Some well-designed M tags can be reused in multiple JSPs or even in different systems. Java Bean is used to control JSP and model objects. JSP uses Java Bean to read data from model objects, while model and controller objects are responsible for updating Java Bean data. In general, you can first design all the screens that may appear, that is, all the content that you can see when using the system. Then, identify the public part, static part, and dynamic part based on the content. You can consider using the template method to generate JSP for the public content separately, and generate HTML or JSP for each content that needs to be changed,
A template JSP dynamically introduces these different parts (include method ). Another issue to be considered is the screen selection. After processing the user request, the template is automatically called to display the screen. Therefore, you must know the components of the screen that the user cares about. Therefore, you can consider placing all screen definitions in a centralized file, such as a Java file or text file. Considering the possibility of future changes to the screen definition file, it is best to use a text file, such as an XML file, so that future changes do not need to be re-compiled. You can map the URL and Parameters entered by the user to a result screen. Of course, you may also need to select different results Screen Content Based on the execution result of the action. Therefore, you need a matching file (XML) for the request and resource. If a URL request has several different results, you need to specify in the file whether flow control is required (a controller object) and corresponding screens with different flow directions.

3.2 Application of Model in Web System

The model object represents business rules and business data, which exist at the EJB layer and web layer. In J2EE specifications, some system data needs to be stored in databases, such as the user's account model and company model, there are also some that do not need to be recorded in the database, such as the current product catalog browsed by a user (catalog model) and his shopping contents (Shopping Cart model. Which layer of the model data exists depends on their lifecycle and scope. On the web layer, httpsession, servletcontext, and Java Bean objects are used to store data, while on the EJB layer, ejbs are used to store data and logic. Java
The bean model object stores the data copy of the model object at the EJB layer. Because the EJB layer has many different model objects, the web layer can use a modelmanager to control model objects in the EJB layer. In modelmanger, the method of using the backend model object can be encapsulated.
It is not appropriate to model all data and rules as ejbs at the EJB layer. For example, you can model the objects used to access the database as Dao objects. Dao can encapsulate interaction details with specific databases, such as reading and writing different tables, multiple databases, and even multiple databases. For example, the model object of an order can be an orderdao, which may process the order table, orderstatus table, and orderitemlines table at the same time.
You can also consider using value objects. A value object can encapsulate remote objects, because the attribute of each read remote object may be a remote process call, which consumes network resources. You can use the value object in the remote object of EJB. You can get the value object in the remote object at one time to get the values of all attributes.

3.3 application of controller in Web System
The Controller object coordinates the model and view to translate user requests into events recognized by the system. At the web layer, there is usually a mainservlet (or main. jsp) that receives all requests. It can call the screen Stream manager (screenflowmanger) to determine the next screen. There is also a request processor requestprocessor, which contains the processing logic required for all requests, such as translating requests into system events ). The request processor usually contains a proxy object clientcontrolwebimpl, which is the Web-layer proxy for the logic processing of the EJB layer. At the EJB layer, a clientcontroller provides the Web
Layer read-only access to the EJB layer. Another statemachine is used to create and delete ejbs and process events sent from the web layer.
Another important function of controller is to synchronize view and model data. The modelmanger contains a modelupdatemanger, which converts system events to a set of models, that is, all models to be synchronized, and notifies listeners to perform synchronization.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.