What is the difference between hexagonal architecture and layered architecture?

View the original article: What is the difference between hexagonal architecture and layered architecture?

What are hexagonal architecture and layered architecture?

Hexagonal Architecture and Layered Architecture are two common software architecture patterns.
The hexagonal architecture emphasizes decoupling core business logic from external dependencies and communicating with the external world through interfaces. The core business logic is at the center of the architecture, and external dependencies are connected to the core business logic through adapters. This architecture has the advantages of high flexibility, easy testing and expansion.
A layered architecture divides a software system into multiple logical layers, each with specific responsibilities and functions. Common layers include presentation layer, application layer, domain layer and infrastructure layer. A layered architecture provides clear separation and organization so that the responsibilities of each layer are clear and easy to understand, test, and maintain.
These two architectural patterns have different application scenarios and advantages in software system design and development. You can choose the appropriate architectural pattern according to specific needs.

Hexagonal architecture diagram:

1b918dbcbb12a294cc74d6adc53f2b98.png

Layered architecture diagram:

25efd0dcd853a875a41701fb34808452.png

Hexagonal architecture and layered architecture are two common software architecture patterns that have some differences in how they organize and divide code.

  1. Hexagonal Architecture is also called Ports and Adapters Architecture. Its core idea is to divide the system into two levels: internal and external.

    The goal of the hexagonal architecture is to keep the core business logic pure and testable, while decoupling external dependencies from the core logic to provide flexibility and scalability.

  • The inner layer (Core) is the core of the application and contains business logic and domain models. It is independent of any external dependencies and is not tied to a specific framework or platform.

  • The external layer (Adapters) is used to handle the interaction between the system and the external world, including user interfaces, databases, external services, etc. It is responsible for converting external input into internally processable data and outputting internal data to a suitable external format.

Layered Architecture is a more traditional architectural pattern that divides the system into multiple horizontal layers. A common layering is usually a three-layer architecture, namely presentation layer (Presentation Layer), business logic layer (Business Logic Layer) and data access layer (Data Access Layer).

The main advantage of a layered architecture is that it provides a clear division of responsibilities between different layers, making the code easier to maintain and extend. Each layer depends on the next layer and communicates through interfaces.

  • The presentation layer is responsible for handling interaction with the user interface, including receiving user input, displaying data, etc.

  • The business logic layer contains the core business logic of the system and handles business rules and processes.

  • The data access layer is used to interact with data stores, including databases, file systems, etc.

What are the application scenarios of hexagonal architecture and layered architecture?

Application scenarios of hexagonal architecture:

  • Complex business logic: Hexagonal architecture is suitable for applications with complex business logic. By decoupling core business logic from external dependencies, business rules and processes can be better managed and tested. High testability requirements: The internal layers of the hexagonal architecture are independent of external dependencies, allowing the core business logic to be unit tested independently without relying on external resources. This improves the testability of the system and enables automated testing more easily.

  • Need for flexibility and scalability: Hexagonal architecture provides greater flexibility and scalability by decoupling internal core logic and external dependencies. When new external adapters need to be added or external dependencies changed, there is not much impact on the internal core logic.

Application scenarios of layered architecture:

  • Simple business logic: The layered architecture is suitable for relatively simple business logic, such as small applications or prototype development. It provides a simple and intuitive way to organize code that is easy to understand and maintain.

  • Clear structure: The layered architecture divides the system into different horizontal layers so that each layer has clear responsibilities and functions. This helps improve code readability and maintainability.

  • Cross-platform compatibility: Layered architectures generally do not rely on a specific framework or platform, but communicate through interfaces. This makes the application more cross-platform compatible and can run and scale in different environments.

It should be noted that hexagonal architecture and hierarchical architecture are not mutually exclusive choices. In actual projects, their characteristics can also be used in combination to design the architecture according to specific needs. Choosing the right architecture depends on the size and complexity of the project and the technical experience of the team.

at last

    Generally, the hexagonal architecture is basically used in DDD. The advantage of the traditional architecture is that it is simple, efficient and easy to use. However, it is really difficult to achieve the strict hexagonal architecture. Even if it is achieved temporarily, it may not work. There may be some discrepancies due to iterations or personnel changes. Of course, that doesn’t mean it’s bad, it just means that in the end it still depends on the scenario.

Reference article:

https://developer.aliyun.com/article/1204103

https://blog.csdn.net/qianshangding0708/article/details/106232926

https://stibel.icu/md/method/arch-principle/arch-principle-arch-develop.html

https://zhuanlan.zhihu.com/p/479800537

https://juejin.cn/post/7034495653333958670

https://insights.thoughtworks.cn/architecture-from-sandwich-to-hexagon/

Guess you like

Origin blog.csdn.net/qq_16498553/article/details/131887043