L3 architecture and MVC

Source: Internet
Author: User
Tags ldap
Introduction to Layer 3

Let's talk about the old topic of web layer-3 architecture. We all know that the three-tier Web architecture refers:

•> User Interface Layer)

•> Business logic layer)

•> Persistence Layer

 

Business logic and user interfaces

In early web development, there was no such layer-3 division because the business was relatively simple. User data presentation and input receiving, encapsulation, verification, processing, and database operations are all stored on the JSP page. At this time, the development is like pangu is not yet ready, and the whole Web development is "Chaotic ". As the business becomes more complex, people begin to consider better using OOP to solve the problem. As a result, some people discovered that the business logic was extracted and formed a layer unrelated to the display and persistence, which made the business logic clear and the product easier to maintain. This is the development method of JSP Model 1 proposed by Sun.

About persistence

In the JSP M1 development method, no suggestions are given on how data is persisted. In many companies, their products are database-centric for architecture and design. Although there are Dao layers in their products, their responsibilities are unclear. Why do we say this? I found that in the eyes of many people, the criticism on the DaO layer is very simple-add, delete, modify, and query. However, in my opinion, this understanding is actually an inverse. This is understandable for the management of simple data. However, as business logic becomes increasingly complex. We have a headache for complicated object relationships. If we need to consider how to store data (usually stored in a relational database ), we began to complain that our software architecture was so disgusting and messy. Object-Oriented Design Ideas teach us how to do this-if we don't want to do it, let us do it for others! At this time, the smart architects proposed the concept of persistence. If we add a new layer in our own application, specifically responsible for the persistent storage and synchronization of object states, can we wholeheartedly "create objects? The emergence of the concept of persistence represents a reduction in our dependency on relational databases. So some people even infer that the database is dead. At the same time, the new concept of relational database is constantly formed and evolved into a theory, and the product is derived from the theory. Therefore, a well-aware programmer should at least agree that persistence is not the most important part of the product-the most important part is the clear and correct business logic.

Benefits of the layer architecture:

 

1. A three-layer logical architecture is used to effectively divide the system into the interface processing layer, business logic layer, and data access layer. In this way, the benefit of dividing layers is that each layer has a relatively independent responsibility, reducing the dependency between layers. Even if a layer changes, it does not affect other layers, thus ensuring the stability of the architecture.

 

2. Separate the interface from the logic code. Even if the page changes, the business logic will not be affected. Therefore, the client interface uses Win form
Or mobile Web form, you only need to create a user interface layer and directly reuse the services provided by the business logic layer.

 

 

Although there are many layer-3 architectures, some problems cannot be solved ======

 

Example 1: Verify and encapsulate form data:

Suppose we are submitting a simple form. We want to verify and encapsulate the data of user data and finally deliver it to the business logic layer as an entity object. From the analysis of the three-tier architecture, what we want to do is:

 

 

But who should do the verification and encapsulation of data? UI Layer or business logic layer? Not suitable!

 

Example 2 if we want to provide different languages for people in different countries and regions, we will undoubtedly need international support. Then, we need to determine the country text that should be presented to the user based on the user's configuration or request information on the JSP page. Should these judgment and display logic be divided into the business logic layer or the UI Layer?

 

Use MVC to solve the problem

For the ambiguous issue, we always have to find a way to break it down. MVC is a design concept. This idea emphasizes the separation of model, view, and controller. How does this idea work on the web? In fact, we introduced the MVC idea in Web development to achieve the goal of separating the UI Layer from the business logic layer-the Controller exists to achieve the above purpose!

After solving the persistence problem, we found that the business logic layer we mentioned is the same as the model in MVC. The UI Layer we mentioned is the same as the view in MVC. MVC provides the idea of separating models from views-introducing controllers. We hand over tasks such as page Jump relationship management, form data encapsulation and verification, and internationalization to the Controller for processing. Therefore, it is not difficult to understand why popular MVC frameworks have the following features: page Jump management, form data encapsulation and verification, and internationalization.

 

Advantages of MVC: low coupling

The view layer and business layer are separated so that you can modify the view layer code without re-compiling the model and controller code. Similarly, to change the business flow or rules of an application, you only need to change the MVC model layer. Because the model is separated 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 separated from the Controller and view, so it is easy to change the data layer and business rules of the application. If you port a database from MySQL to Oracle, or change the data source from RDBMS to LDAP, you only need to change the model. Once the model is correctly implemented, the view correctly displays the data from the database or LDAP server. Since the three components of an application using MVC are independent of each other, changing one of them will not affect the other two, a well-coupled component can be constructed based on this design idea.

High reusability

As technology advances, more and more methods are needed to access applications. The MVC mode allows different views to access code on the same server, because multiple views share one model, including any web (HTTP) browser or wireless browser (WAP). For example, you can use a computer or mobile phone to order a certain product. Although the ordering method is different, the ordering method is the same. Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be expressed in HTML, but may also be expressed in WAP. The commands required to represent these data are to change the implementation method of the view layer, the control layer and model layer do not need to be changed. Since data and business rules have been separated from the presentation layer, code can be reused to the maximum extent. The model also provides State management and data persistence processing functions. For example, session-based shopping carts and e-commerce processes can also be reused by flash websites or wireless networking applications.

Low lifecycle costs

MVC reduces the technical content of developing and maintaining user interfaces.

Fast deployment

Using the MVC pattern, the development time is greatly reduced. It enables programmers (Java developers) to focus on business logic and interface programmers (HTML and JSP developers) focus on the form of expression.

High maintainability

Separating the view layer and business logic layer also makes Web applications easier to maintain and modify.

Favorable Software Engineering Management

Because different layers perform their jobs, different applications have some same features, which is conducive to managing program code through engineering and tools. The Controller also provides the advantage of using controllers to connect different models and views to meet user needs. In this way, the controller can provide a powerful means to construct applications. Given some reusable models and views, the controller can select a model for processing based on the user's needs, and then select a view to display the processing results to the user.

The shortcomings of MVC are not clearly defined.

It is not easy to fully understand MVC. Using MVC requires careful planning. Because of its complicated internal principles, it takes some time to think about it. At the same time, due to the strict separation of models and views, it also brings some difficulties to debug the application. Each component must be thoroughly tested before use.

Not suitable for small and medium-sized applications

A large amount of time to apply MVC to a large-scale application is usually not worth the candle.

Increase system structure and implementation complexity

For a simple interface, strictly abide by MVC. Separating Models, views, and controllers increases the complexity of the structure and may lead to too many update operations to reduce the running efficiency.

Too close connection between views and controllers

Views and controllers are separated from each other, but they are closely related components. A view does not have a controller, and its applications are limited. Vice versa, which hinders their independent reuse.

Inefficient access to model data from a view

Depending on the model operation interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also damage operation performance.

Generally, Advanced Interface tools or constructors do not support mode

Modifying these tools to adapt to MVC requires a high cost of building separate components, which may cause difficulties in using MVC.

 

 

Summary

In Java Web development, the MVC framework acts as an adapter for the UI Layer and business logic layer. The MVC Framework maximizes the separation between the UI Layer and the business logic layer.

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.