Abstract Object-oriented design model is a summary of experience, MVC thinking is originally used to build user interface. This article mainly discusses how to use design patterns and MVC architectures in new web applications. The article first introduces the concept and characteristics of design patterns, and the design ideas of the MVC architecture, and analyzes several major patterns included in the MVC architecture. Then, according to the characteristics of the web application system, some design ideas are put forward on how to apply models and MVC architecture. 1. Introduction 1.1 The emergence and application of object-oriented technologies have greatly improved software reuse and software quality. Object-oriented programming is also simpler and efficient than past programming mode, but object-oriented design methods should be complicated and more technically, a good design should have a targeted problem. It is also fully considering sufficient versatility for future issues and demand. In the past ten years, people have created some good solutions for some problems in research exploration and practical applications against the surface-to-object technology, namely the so-called object-oriented design pattern. One of the objects of object-oriented technology is to improve the reuse of software, and reuse the design mode, the reuse of design is reflected in the significance and essence of reuse from deeper levels. People have a lot of definitions for design patterns, which is the most definition of Christopher Alexander's design mode: Each design pattern is a tripartite rule that expresses a context, a problem and a solution. Design patterns generally have the following basic elements: pattern name, problem, purpose, solution, effect, sample code, and related design patterns. There are several categories of design patterns, which can be divided into three kinds of creative, structural, and behavior (Behavioral) based on its purpose. The creation mode is mainly used to create objects. The structural mode is primarily a combination of classes or objects, and behavioral modes are mainly used to describe how to interact with class or objects and how to assign duties. It is also possible to divide the design mode into a relationship between class mode and object mode, class mode processing class, and subclass according to the scope. These relationships are built by inheriting, and it is determined in compilation time and is static. Object mode is the relationship between processing objects, which changes in runtime, more dynamic. The feature of the mode: It is obtained by experience, written down with some structured format, avoiding the same problem of weight design, existing abstraction layer, constantly being reusable, reusable artificial product, make Design and best practice interactions to solve greater problems in combination. 1.2 The MVC architecture MVC is initially used in SmallTalk-80 to build user interface. M represents model Model, V represents View View, C represents the controller controller. The purpose of MVC is to increase the reuse rate of the code, reduce the coupling of data expression, data description, and application operations. It also improves software maintainability, removability, scalability, flexibility, and encapsulation. Single users' applications are usually organized as an event-driven user interface. Developers draw a user interface interface with an interface tool, then write code to perform the corresponding action according to user input, and many interactive development environments encourage this because it emphasizes the interface and then have functions. Some software design patterns are like this, and then the fixed code is often integrated into the last system. The result is that the program organization surrounds the user interface elements and the user's operations on those interface elements, the data of the data store, the application function, and the code used to display the code that is coming together. The code structure in a single user's system is like this because the system needs will not change frequently. But it is not applicable to a large system such as a large Web system, or e-commerce system. The design of the distributed system can be improved by separating data patterns from various data that can be accessed and controlled.
The MVC design pattern consists of three parts. The model is an application object, no user interface. The view indicates that it is displayed on the screen that represents data flowing to the user. The controller defines the response mode of the user interface to the user input, which is responsible for converting the user's action into operations for Model. Model reflects changes in data by updating the data of the view. Three relationships are shown in the figure: Method for MVC Relationship Diagram Figure 2 MVC Division and Collaboration 2. Design mode in MVC A lot of design patterns with MVC architectures, but the most closely related to MVC is the following three Mode: Observer, Composite, and Strategy. 2.1 OBServer mode MVC separates Model and View by using ordered / notifications. View To ensure that you can display the contents and status of Model you can correctly reflect. Once the contents of Model have changed, there must be a mechanism to make the Model to notify the relevant view so that the relevant View can refresh the data in the appropriate timing. This design can also solve more general problems, separating objects, making changes to other objects, and this object does not know the details of those affected objects. This is designed as the OBServer design mode. Mode Type: Observer mode is an object mode, and it is also a behavior pattern. Mode purpose: Define a pair of dependencies between objects, when the value or status of an object changes, all objects with its dependency relationship are notified and automatically updated. A certain data may have a variety of display methods and may also be displayed in different ways (Figure 2) simultaneously. When data is changed by one way, then other displays should be able to immediately know the changes in data and make corresponding adjustments. Mode structure: Figure 3. Structural map effect of Observer mode: 1. Abstract coupling. The target object only knows that it has some observer, each of which is a simple interface of an abstract OBServer class, and does not know which class they specifically belong. This makes the coupling between the target and the observer and the smallest and abstraction. 2. Support broadcast communication. The target transmission notification does not have to specify the observer, how to handle the notification by the observer. 3. Possible unexpected updates. To handle update logic, avoid error updates. 2.2 Composite Mode MVC An important feature is that View can nested. Nested Combined Views can be used in any way available, and can manage nested views. This idea reflects the design of the combined view with its components equally. This design idea is described in object-oriented domain to become a design pattern of Composite. Mode Type: Composite mode is an object mode, and it is also a structural mode. Mode Objective: To combine the object into a tree structure to represent a "partial" hierarchy. Composite has consistency for the use of the combined object and the use of a single object. Mode structure: Figure 4. Composite mode structure pattern effect: 1. Define a class hierarchy containing simple objects and combined objects. Simple objects can be combined into complex objects, while combined objects can be combined. Such a combined object can be used in such a way that the simple object is used in the client code. 2. Simplify client code. The client doesn't have to know that an object is a simple object or a combined object, which can be used in a consistent manner. 3. It is easier to add new types of components. The new component can easily add that the client code does not have to change the client code. 2.3 Another important feature of the Strategy Mode MVC is that the response method for the user can be changed to the user can be changed without changing the View. This is very important for a system that often needs to change response logic. MVC encapsulates the response logic in Controller. There is a class hierarchy of Controller that can easily change the original Controller, create a new Controller. View uses the Controller subclass to implement a specific response policy. To achieve different response policies, just replace with different types of Controller instances.
You can also change the response policy entered by the user by changing the VIEW's Controller at runtime. This View-Controller relationship is an example of a design pattern that is described as Strategy. Mode Type: Strategy mode is an object mode, and it is also a behavior pattern. Mode Purpose: Define a series of algorithms and package them, allowing them to replace each other, making algorithms can vary independently of its clients. Mode structure: Figure 5. Structural map of Strategy mode: 1. The Strategy class level defines reusable related algorithms or behaviors for Context. 2. Alternative inheritance methods. If you directly inherit the context, give a different behavior to the Context to mix the implementation of the algorithm with the context, making context difficult, maintenance, and expansion, and cannot dynamically change the algorithm. The algorithm is encapsulated in a separate Strategy class, which allows the algorithm to change independently, easy to switch extensions. 3. Can provide different implementations of the same behavior. 4. The client must understand what is different between Strategy. 5. Communication overhead between context and strategy. 6. Add the number of objects. 3. Applications of MVC in a Web System Some web-based distributed systems such as B2B e-commerce systems are suitable for use with MVC architecture. Through analysis, an object can be divided into three categories from a high level angle. One is the object responsible for display, and a class of objects contains business rules and data, and one is to receive requests, control business objects to complete requests. The display of these applications is often required, such as the style, hue, and the content that needs to be displayed, and the content of the content is displayed. Business rules and data are relatively stable. Therefore, the displayed object VIEW often needs to change, indicating that the commercial rules and data object models are relatively stable, and the Controller indicating the control is most stable. Usually when the system is released, the View object is managed by the artist, HTML / JSP designer or system administrator. Controller objects are developed by application developers, business rules, and commercial data objects are completed by developers, domain experts, and database administrators. Display logic is controlled at the web layer or client, can be a servlet or JSP, dynamically generate HTML. In general, JSP is better than using servlet. JSP better divides the code from the HTML part, facilitates the separation of page designers and code developers and improves efficiency. At the same time, JSP can complete all Servlet completion, actually JSP is ultimately converted into a servlet. Objects related to control exists at every level of the system, coordinating cross-layer action. Objects containing business rules and data exists in EJB layers (centered on EJB) or web layer (centered in web). 3.1 VIEW Application VIEW represents the display of the system, which is completely present in the web layer. It is generally composed of JSP, Java Bean, and Custom Tag. JSP can dynamically generate web content, Custom TAG is more convenient to use Java Bean, and it can package display logic, more conducive to modular and reuse. Some design good Custom Tags can be reused in multiple JSPs even in different systems. Java beans are used to control JSP and Model objects. JSP reads the data in the Model object through Java Bean, and the Model and Controller objects are responsible for data updates for Java Beans. In general, you can need to design the possible screens, that is, all the content you can see when the user uses the system.
Then, according to these content, find the common part, static part, and dynamic variations. You can consider using a template method, separately generate JSP separately, and each generates HTML or JSP, which is generated by a template JSP, which is dynamically introduced to the INCLUDE method. There is also a question to consider is the selection problem of the screen. When the user request is processed, the template is automatically called to display, this display must know which part of the user cares. So you can consider putting all the definitions of all screens in a centralized file, such as a Java file or text file. Because of the possibility of a screen definition file, it is best to use a text file such as an XML file so that the future changes do not have to be recompiled. You can map to a result screen according to the URL and parameters entered by the user, of course, it is possible to select different result screen content according to the execution result of the action. So there is a need for a request and resource matching file (XML) if there are several different results in a URL request, and you need to indicate whether stream control (a Controller object) and the corresponding screen of different flows. 3.2 Model's Application Model object in the web system represents business rules and business data, exists in the EJB layer and the web layer. In J2EE specifications, some data need to be stored in the database, such as user account information (Account Model), company data (COMPANY MODEL), etc., there are no need to record in the database, such as a user browsing. Product Catalog (Catalog Model), Shopping Cart Model, etc. Which of these model data is present to determine according to their life cycle and scope. There is an HttpSession and ServletContext and Java bean objects in the Web layer to store data, and EJB is stored in the EJB layer to store data and logic. The MODEL object of the Java Bean of the Web layer stores the copy of the data of the EJB layer Model object. Because the EJB layer has a lot of different model objects, the web layer can control each model object of the EJB layer through a ModelManager, and can encapsulate the method of using the background Model object in ModelManger. It is also inappropriate to modify all data and rules to EJB in the EJB layer. If you can use the object mode of the access database to a DAO object. DAO can be packaged with the specific database of the specific database, such as reading and writing different tables, multiple databases, and even a variety of databases. If the order of the Model object can be an ORDERDAO, it might want to handle the Order table, the ORDERSTATUS table, and the OrderItemLines table. There is also a Value object. A Value object can package remote objects because the properties of each read remote object may be a remote procedure call, which will cost network resources. You can use the Value object in the remote object of the EJB. You get the value object in a remote object to get all the values of all properties. 3.3 Controller The application controller object in the web system coordinates the Model and View to translate the user into the system identified. In the web layer, there is typically a MainServlet (or main.jsp), receive all requests, which can call the screen stream manager (ScreenflowManger) decide the next screen. There is also a request processor requestProcessor, which contains all requests need to be done, such as translating requests into system events. The request processor typically also contains a proxy object ClientControlWebimpl, which is a logic of the EJB layer. The agent of the Web layer.