JavaEE - Introduction to the Three-Tier Architecture Pattern

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Introduction to the three-tier architecture model

Three-tier architecture pattern:

Three-tier architecture (3-tier architecture) The three-tier architecture in the usual sense is to divide the entire business application into: the interface layer (User Interface layer), the business logic layer (Business Logic Layer), the data access layer (Data access layer). The purpose of distinguishing layers is the idea of ​​"high cohesion and low coupling". In software architecture design, the layered structure is the most common and the most important one. The hierarchical structure recommended by Microsoft is generally divided into three layers, from bottom to top: data access layer, business logic layer (also called domain layer), and presentation layer.

Presentation layer:

The interface layer, also known as the presentation layer, is the outermost (topmost) layer, closest to the user. It is used to display data and receive data input by users, and provide users with an interactive operation interface.

Business logic layer:

The Business Logic Layer is undoubtedly the part that reflects the core value of the system architecture. Its focus is mainly on system design related to business requirements, such as the formulation of business rules and the realization of business processes, that is to say, it is related to the domain logic that the system deals with. The layer is called the domain layer. For example, Martin Fowler, in his book "Patterns of Enterprise Application Architecture", divides the entire architecture into three main layers: presentation layer, domain layer and data source layer. As a pioneer of domain-driven design, Eric Evans has made a more detailed division of the business logic layer, subdivided into the application layer and the domain layer, and further separated the domain logic from the solution of the domain logic through layering. The position of the business logic layer in the architecture is very critical. It is located in the middle of the data access layer and the presentation layer, and plays the role of linking the previous and the next in the data exchange. Since the layer is a weakly coupled structure, the dependencies between layers are downward, the bottom layer is "ignorant" to the upper layer, and changing the design of the upper layer has no effect on the bottom layer it calls. If the idea of ​​interface-oriented design is followed in the hierarchical design, then this downward dependency should also be a weak dependency. Therefore, on the premise of not changing the interface definition, the ideal layered architecture should be a "drawer" architecture that supports extraction and replacement. Because of this, the design of the business logic layer is particularly critical for an architecture that supports extensibility because it plays two distinct roles. For the data access layer, it is the caller; for the presentation layer, it is the callee. The relationship between dependencies and dependents is entangled in the business logic layer. How to realize the decoupling of dependencies is a task left to designers in addition to implementing business logic.

Data Access Layer:

The data access layer, sometimes referred to as the persistence layer, is mainly responsible for database access, which can access database systems, binary files, text documents or XML documents. In short, it implements the operations of select, insert, update and delete on the data table. If you want to add elements of ORM, it will include the mapping between objects and data tables, and the persistence of object entities.

The difference between three layers and MVC:

Many people tend to confuse the three-tier pattern with the MVC pattern. The most difference between the three-tier and MVC is that the three-tier model does not have the concept of a Controller. Although the same is at the architectural level, the three layers are the same as MVC in that they all have a presentation layer, but their difference lies in the other two layers. MVC does not regard business logic access as two layers, which is the main difference between using a three-tier architecture or MVC to build a program. Of course, the concept of Model is also mentioned in the three-tier architecture, but the concept of Model in the three-tier architecture is different from the concept of Model in MVC. The typical Model layer in the "three-tier" is composed of entity classes, while MVC It is composed of business logic and access data.

In the three layers, both JSP and Servlet code belong to the presentation layer, the business logic layer is the entity class that completes business rules, and the data access layer is JDBC and other codes. The schematic diagram:

image

image

The three-tier architecture separates the business responsibilities of different layers more thoroughly. The logic layer does not contain a little bit of view layer code, and the same data layer should not contain a little bit of logic layer code, because if it contains code from other layers, it cannot To achieve complete decoupling, there is still a certain degree of coupling.

The three-tier architecture better realizes modular programming, and the system designed with the three-tier architecture is easier to expand and replace, especially now that there is more than one device on the PC side, and if the layering is not done well, it cannot adapt to the access of multiple devices. For example, we use jsp+Servlet for the presentation layer, which is oriented to the web. If we don’t do web any more, we need to replace the entire presentation layer with desktop graphics for display. If we use a three-tier architecture, we only need to replace the presentation layer. Layers are sufficient, and both the logic layer and the data layer can be reused. If there is no layering, all modules are coupled together and cannot be reused, and we can only re-write a system that adapts to the desktop, which is very time-consuming and labor-intensive.

We all know that WebService is a remote calling technology across programming languages ​​and operating system platforms. If a system is designed with a three-tier architecture, the logic layer can be shared with other applications written in different languages ​​through WebService.

The recently popular WeChat applet accesses the server through https, it needs the server to return json data, then we can receive this access in the servlet in the view layer, and return the json data after processing.

Schematic:

image

Advantages and disadvantages of the three-tier development model

advantage:

1. Developers can only focus on one of the layers in the entire structure;

2. It is easy to replace the original level of implementation with a new implementation;

3. It can reduce the dependency between layers;

4. Conducive to standardization;

5. It is beneficial to the multiplexing of logic at each layer.

6, the structure is more clear

7. In the later maintenance, the maintenance cost and maintenance time are greatly reduced

shortcoming:

1. Reduce the performance of the system. This goes without saying. If the hierarchical structure is not adopted, many businesses can directly access the database to obtain the corresponding data, but now it must be done through the middle layer.

2. Sometimes leads to cascading modifications. This modification is especially reflected in the top-down orientation. If a function needs to be added in the presentation layer, in order to ensure that its design conforms to the hierarchical structure, it may be necessary to add corresponding codes in the corresponding business logic layer and data access layer.

For example, if a restaurant adds a dish, the menu (UI), the chef (BLL), and the purchase (DAL) must be processed accordingly

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326351866&siteId=291194637