Layered Development and Clean Architecture in Java Development

Layered development (horizontal split)

The concept of layered development:

  • Maven multi-module development project management.
  • This management function can be used to realize multi-level module development of a project - layered development.
    For example, the current project HelloController depends on HelloService
    insert image description here
  • The purpose of doing this: Complex development process. Decoupling (do not adjust dependencies, cannot be decoupled).
  • The difference between layered development (horizontal split) and vertical split is that splitting out multiple layers and finally running it is also a project.

clean architecture

<<The way to clean code>>The author Uncle Bob once said that
the translation:
the program structure is always the same.
It is very simple to make the program run.
It is difficult to make the program "correct".
To make the program easy to maintain and expand is correct
. Take service-mapper as an example, develop in layers according to intuition, and make dependencies.
insert image description here
Question 1: The control layer is not implemented, and the isolation relationship between the persistence layers can be injected in the controller at will, depending on the mapper.
insert image description here
Question 2: Architecture layering Between is purely strong coupling.
insert image description here
Layered development did not achieve the ultimate goal, to achieve decoupling, to achieve easy expansion and maintenance.
Corresponding to the above problems, in Uncle Bob's <<The Way of Clean Architecture>>, mentioned the solution idea.

Clean Architecture Landing Solution

Core point: Among the many hierarchical modules, there is the core business module (service).

Other modules, including controller, redis, rocketmq, mysql, and mybatis, are easily replaced.
If a core stable module relies on an unstable module that is easy to change, it does not satisfy the idea of ​​​​a clean architecture.
Solve Solution: Dependency Inversion (development principle)

Dependency Inversion

  • The Dependency Inversion Principle (DIP) is an important principle in object-oriented design. It mainly contains two core concepts:
    1. High-level modules should not depend on the concrete implementation of low-level modules, but on abstractions.
    2. Abstractions should not depend on concrete implementations, but on higher-level abstractions.
  • In short, the Dependency Inversion Principle advocates that both high-level and low-level modules of a program should depend on abstractions, not concrete implementation details. In this way, each module of the system can be decoupled to achieve the design effect of easy expansion and high flexibility.

Here are some ways to practice the Dependency Inversion Principle:

  • Dependency Injection (Dependency Injection, DI): Through dependency injection, dependencies are moved from inside the code to external containers for management. Define dependencies through interfaces or abstract classes, and inject concrete implementations into users through constructors, properties, or method parameters.
  • Interface programming: use interfaces or abstract classes as the contract between modules, so that the dependency relationship between high-level modules and low-level modules is established on abstraction, not on specific implementation classes.
  • Interface-oriented programming: In the design and development process, try to use interfaces to define the interaction between modules instead of directly depending on specific classes. This improves the flexibility and maintainability of the system.
  • By following the Dependency Inversion Principle, the testability, scalability, and decoupling of the code can be improved, making the system easier to maintain and understand.

Guess you like

Origin blog.csdn.net/m0_72568513/article/details/131948421