Evolution of Java Web Program Architecture Patterns

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.

Evolution of Java Web Program Architecture Patterns

The older generation of programmers have generally experienced the evolution of the Web program architecture model, from the initial development on jsp or jsp+Servlet, to the later mvc, three-tier and so on. And now there are quite a few people who have finished learning the web, and they may not have used jsp or jsp+Servlet to develop projects, so they directly learn to use Spring, Spring Boot or SpringMVC and other frameworks for development. If we don’t go through such a gradual evolution process, it is difficult to understand what kind of benefits the framework brings to us, and it is difficult to solve problems encountered in the development process, let alone learn the source code in these frameworks. Learning is a gradual process and cannot be rushed, so this article aims to briefly talk about the development history of the Web.

A brief history of web development

Taking the current Spring Boot as the timeline, the process of web development can be roughly divided into the following stages: 1. Pure jsp / jsp+Servlet / jsp+JavaBean+Servlet
2. MVC / MVP / three-tier architecture
3. Using EJB For the development of distributed applications, EJB is a heavyweight framework, which is more complicated and troublesome to use.
4. Because EJB is too heavy, Spring came into being, but Spring is getting more and more bloated in development, so there are still many tedious
configurations5 . Similarly, because String configuration is too cumbersome, Spring boot was born, and then you can experience the fun of "convention is greater than configuration"

Second, the initial stage of web development

1.jsp / jsp+Servlet / jsp+JavaBean+Servlet development mode:

At the very beginning, jsp just came out, and web development at that time was basically done on jsp+JavaBean. What's more, the page, logic, and data processing are all written directly on the jsp. I want to know that the project code developed in this way is not only messy, but also has a high coupling, which makes the project difficult to maintain.

The pure JSP flow chart is as follows:

image

The flow chart of JSP+JavaBean is as follows:

image

I believe that many people have used the above two modes of development more or less when they first started learning JavaWeb. Needless to say, the first mode is not to mention. All the code is written on JSP and the coupling degree is quite high. Although the second mode is decoupled to a certain extent compared to the first mode, JSP is still responsible for page control and processing of requests and responses, and the responsibilities are not single. The degree of coupling is still relatively high. The result of high coupling is that the JSP code is very complicated and confusing, and it is difficult to maintain later.

Although the above mode is relatively simple to develop, it can also be suitable for the development of some small projects at that time, but it is gradually eliminated due to the difficulty of code confusion and maintenance.

After learning the pain of this mode, I began to add Servlet. At this time, JSP is only responsible for page control, Servlet is responsible for data verification, and JavaBean is responsible for specific business logic and data processing, encapsulation, and database interaction.

The flow chart of JSP+JavaBean+Servlet is as follows:

image

In this pattern has begun to have a little shadow of MVC, but this pattern can not be called a relatively complete MVC design pattern. Compared with the previous two modes, this mode has a clearer division of labor, extracts the Servlet layer, and reflects a simple layering idea.

mind Mapping:

image

3. The primary and intermediate stages of web development

1. MVC architectural pattern

At this time, the MVC architecture pattern has also been applied in web development, although MVC is no longer a new architecture pattern, and the MVC pattern has existed as early as the era of desktop development. MVC is the abbreviation of Model, View, and Controller. MVC decomposes the system into three parts: model, view, and controller. Each part is relatively independent and has a single responsibility. In the implementation process, it can focus on its own core logic. MVC is a reasonable sorting and segmentation of system complexity, and its essence is "separation of concerns". As for the division of responsibilities and interrelationships of the three elements of MVC, I will not repeat them here. The following figure gives a very detailed description:

image

The above diagram illustrates the functions and relationships of MVC components. The communication between the various parts of the MVC pattern is as follows:

image

The View sends instructions to the Controller. After completing the business logic, the Model is required to change the state. The Model sends new data to the View, and the user gets feedback. All communication is one-way.

When accepting user instructions, MVC can be divided into two ways. One is to accept the command through the View and pass it to the Controller. The flow chart is as follows:

image

The other is to accept instructions directly through the controller. The flow chart is as follows:

image

Generally, a more flexible method is often used in practical projects, and these two methods are usually combined together. The general flow chart is as follows:

image

1. The user can send an instruction (page request) to the View.

2. Users can also send instructions (Servlet requests) directly to the Controller.

SpringMVC is now the framework of the MVC architectural pattern.

MVP architectural pattern:

MVP is very similar to MVC. MVP is evolved from the classic MVC model. Their basic ideas are similar: Controller/Presenter is responsible for logic processing, Model provides data, and View is responsible for display. Therefore, many people are not very clear about the difference between these two modes. In short, the main difference between the two is that MVC is one-way communication, while MVP is two-way communication. The MVP mode renamed the Controller to the Presenter, so the communication direction was changed at the same time. The flow chart is as follows:

image

MVP Features:

The communication between each part is two-way.
View and Model do not contact, are passed through Presenter.
View is very thin and does not deploy any business logic, it is called "Passive View" (Passive View), that is, it does not have any initiative, while Presenter is very thick and most of the main logic is deployed there.

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.

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

Several architectural patterns have been introduced above. It can be seen that the evolution purpose of architectural patterns is decoupling. Only a low-coupling architecture can facilitate the maintenance and expansion of the project in the later stage, and a good architectural pattern can make the project have better robustness. .

Fourth, the advanced stage of web development

This stage begins to use EJB for distributed application development:

EJB is sun's JavaEE server-side component model, and the design goal and core application is to deploy distributed applications. Simply put, it is to package the programs (ie: classes) that have been written and put them on the server for execution. With the advantage of java cross-platform, the distributed system deployed with EJB technology can not be limited to a specific platform. EJB (Enterprise JavaBean) is part of J2EE (javaEE) and defines a standard for developing component-based enterprise multi-applications. Features include web service support and core development tools (SDKs). In J2EE, Enterprise Java Beans (EJB) is called Java Enterprise Bean, which is the core code of Java, namely Session Bean (Session Bean), Entity Bean (Entity Bean) and Message Driven Bean (MessageDriven Bean). After the launch of EJB3.0, the entity bean was separated out separately, forming a new standard JPA.

EJB is not technically a "product", EJB is a standard that describes the construction of application components to solve: Scalable
(Distributed)
Transactional (Transactional)
Data storage (Persistent)
Security ( Secure)

The above is transferred from Baidu Encyclopedia.

Since I personally have not used EJB for development, I dare not express my opinion casually, so as not to mislead everyone. If you are interested in EJB, you can refer to the following articles:

http://www.uml.org.cn/j2ee/2009112011.asp

The birth of Spring:

Rod Johnson's 2002 book Expert One-to-One J2EE Design and Development, in which Rod questions the bloated, inefficient, and disconnected academic practices of the J2EE Orthodox Framework (EJB), And this book as the guiding ideology, wrote the interface21 framework, which is later Spring.

Rod Johnson is credited with establishing the Spring framework based on best practices and suitable for various application types. These ideas are also elaborated in his book. After the book is published, at the request of readers, the source code is made available under an open source license.

A group of developers who volunteered to extend the Spring framework formed a team to build a project on Sourceforge in February 2003. After working on the Spring framework for a year, the team released the first version (1.0) in March 2004. Since this release, the Spring Framework has become extremely popular in the Java community, in part due to its better-than-average documentation capabilities and references, especially for an open source project.

Spring solves the problem of loose coupling between the business logic layer and other layers, so it applies the interface-oriented programming idea throughout the entire system application. In short, Spring is a layered JavaSE/EEfull-stack (one-stop) lightweight open source framework.

Features of Spring Framework:

Lightweight
Inversion of Control
Aspect-Oriented
Container
Framework
MVC

Spring Architecture Overview:

image

Although Spring is much lighter than EJB, it is a bit bloated and cumbersome to configure, but Spring is still one of the mainstream frameworks. The existence of the framework is to facilitate us to use some architectural patterns, and it is not necessary to start development from the bottom, which improves the efficiency of development.

V. The current stage of web development

So far, there have been many excellent Java open source frameworks, such as Spring, SpringMVC, Spring Boot, Struts, Hibernate, MyBatis, etc. Among them, Spring Boot is a simplification of the Spring framework. Through these frameworks, we can efficiently apply architectural patterns to develop large-scale projects.

To simplify, the Spring Boot framework:

Spring Boot is a new framework provided by the Pivotal team, designed to simplify the initial setup and development of new Spring applications. The framework uses a specific way to configure, so that developers no longer need to define boilerplate configuration. In this way, Spring Boot aims to be a leader in the burgeoning field of rapid application development.

Spring Boot features:

Can create standalone Spring applications
Embed Tomcat without deploying WAR files
Simplify Maven configuration
Auto-configuration Spring
provides production-ready features like metrics, health checks and external configuration
Absolutely no code generation and no configuration XML

Although the purpose of Spring Boot is to simplify Spring, it seems that there is no need to learn the tedious configuration of Spring. How good is it to learn Spring Boot directly. The configuration is simple and beautiful, but if you have not endured the tedious configuration of Spring, you have not experienced the evolution of architectural patterns and If the JavaWeb foundation is not solid, it is easy to encounter errors that have not been encountered in the process of using Spring Boot, and do not know how to solve them. And if you are not familiar with design patterns, you don't know how other people's frameworks are implemented, and you don't know the design ideas at all, so even if you have a good framework in hand, you can't play 6, let alone design architectural patterns.

6. Summary

From the above brief history of evolution, we can see how these mainstream frameworks came from and why they exist. It can be said that our current era of learning technology has caught up with the best era. Now there are so many excellent open source frameworks that can be used, and so many design ideas can be used for reference. We have skipped a lot of pits that our predecessors often stepped on. It is only after the predecessors stepped on these pits that so many excellent open source frameworks and design ideas can be developed. But we should also go through the evolution of this architectural pattern in order to deeply appreciate the benefits brought to us by different architectural patterns and frameworks. Now there are more and more new things, you and I can only keep up with this era if you keep making progress and keep learning.

Guess you like

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