mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
docs: update acl
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
---
|
||||
title: Anti-corruption layer
|
||||
title: Anti-Corruption Layer
|
||||
category: Integration
|
||||
language: en
|
||||
tag:
|
||||
- Architecture
|
||||
- Decoupling
|
||||
- Integration
|
||||
- Isolation
|
||||
- Layered architecture
|
||||
- Migration
|
||||
- Modernization
|
||||
- Refactoring
|
||||
- Wrapping
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@@ -20,21 +26,21 @@ Implement a façade or adapter layer between different subsystems that don't sha
|
||||
|
||||
## Explanation
|
||||
|
||||
### Context and problem
|
||||
Real-world example
|
||||
|
||||
Most applications rely on other systems for some data or functionality. For example, when a legacy application is migrated to a modern system, it may still need existing legacy resources. New features must be able to call the legacy system. This is especially true of gradual migrations, where different features of a larger application are moved to a modern system over time.
|
||||
> Imagine a large retail company transitioning its inventory management system from an old legacy software to a new modern platform. The legacy system has been in use for decades and contains complex business rules and data formats that are incompatible with the new system. Instead of directly connecting the new system to the legacy one, the company implements an Anti-Corruption Layer (ACL).
|
||||
>
|
||||
> The ACL acts as a mediator, translating and adapting data between the two systems. When the new system requests inventory data, the ACL translates the request into a format the legacy system understands, retrieves the data, and then translates it back into a format suitable for the new system. This approach ensures that the new system remains unaffected by the intricacies of the legacy system, preventing corruption of data and business logic while facilitating a smooth transition.
|
||||
|
||||
Often these legacy systems suffer from quality issues such as convoluted data schemas or obsolete APIs. The features and technologies used in legacy systems can vary widely from more modern systems. To interoperate with the legacy system, the new application may need to support outdated infrastructure, protocols, data models, APIs, or other features that you wouldn't otherwise put into a modern application.
|
||||
In plain words
|
||||
|
||||
Maintaining access between new and legacy systems can force the new system to adhere to at least some of the legacy system's APIs or other semantics. When these legacy features have quality issues, supporting them "corrupts" what might otherwise be a cleanly designed modern application. Similar issues can arise with any external system that your development team doesn't control, not just legacy systems.
|
||||
> The Anti-Corruption Layer design pattern protects a system from the complexities and changes of external systems by providing an intermediary translation layer.
|
||||
|
||||
### Solution
|
||||
[Microsoft's documentation](https://learn.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer) says
|
||||
|
||||
Isolate the different subsystems by placing an anti-corruption layer between them. This layer translates communications between the two systems, allowing one system to remain unchanged while the other can avoid compromising its design and technological approach.
|
||||
> Implement a façade or adapter layer between different subsystems that don't share the same semantics. This layer translates requests that one subsystem makes to the other subsystem. Use this pattern to ensure that an application's design is not limited by dependencies on outside subsystems. This pattern was first described by Eric Evans in Domain-Driven Design.
|
||||
|
||||
### Programmatic example
|
||||
|
||||
#### Introduction
|
||||
**Programmatic Example**
|
||||
|
||||
The example shows why the anti-corruption layer is needed.
|
||||
|
||||
@@ -44,7 +50,7 @@ The aforementioned systems have different domain models and have to operate simu
|
||||
|
||||
But for that, the system needs to know the domain model of the other system and to avoid that, the anti-corruption layer(ACL) is introduced. The ACL is a layer that translates the domain model of the `Legacy` system to the domain model of the `Modern` system and vice versa. Also, it hides all other operations with the other system, uncoupling the systems.
|
||||
|
||||
#### Domain model of the `Legacy` system
|
||||
Domain model of the `Legacy` system:
|
||||
|
||||
```java
|
||||
public class LegacyOrder {
|
||||
@@ -56,7 +62,7 @@ public class LegacyOrder {
|
||||
}
|
||||
```
|
||||
|
||||
#### Domain model of the `Modern` system
|
||||
Domain model of the `Modern` system:
|
||||
|
||||
```java
|
||||
public class ModernOrder {
|
||||
@@ -79,7 +85,7 @@ public class Shipment {
|
||||
}
|
||||
```
|
||||
|
||||
#### Anti-corruption layer
|
||||
Anti-corruption layer:
|
||||
|
||||
```java
|
||||
public class AntiCorruptionLayer {
|
||||
@@ -101,9 +107,7 @@ public class AntiCorruptionLayer {
|
||||
}
|
||||
```
|
||||
|
||||
#### The connection
|
||||
|
||||
Wherever the `Legacy` or `Modern` system needs to communicate with the counterpart the ACL needs to be used to avoid corrupting the current domain model. The example below shows how the `Legacy` system places an order with a validation from the `Modern` system.
|
||||
The connection between the systems. Wherever the `Legacy` or `Modern` system needs to communicate with the counterpart the ACL needs to be used to avoid corrupting the current domain model. The example below shows how the `Legacy` system places an order with a validation from the `Modern` system.
|
||||
|
||||
```java
|
||||
public class LegacyShop {
|
||||
@@ -137,8 +141,8 @@ Use this pattern when:
|
||||
|
||||
## Tutorials
|
||||
|
||||
* [Microsoft - Anti-Corruption Layer](https://learn.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer)
|
||||
* [Amazon - Anti-Corruption Layer](https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/acl.html)
|
||||
* [Anti-Corruption Layer (Microsoft)](https://learn.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer)
|
||||
* [Anti-Corruption Layer Pattern (Amazon)](https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/acl.html)
|
||||
|
||||
## Known Uses
|
||||
|
||||
@@ -162,11 +166,12 @@ Trade-offs:
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* [Facade](https://java-design-patterns.com/patterns/facade/): The Anti-Corruption Layer can be seen as a specialized form of the Facade pattern that is used to isolate different subsystems
|
||||
* [Adapter](https://java-design-patterns.com/patterns/adapter/): The Anti-Corruption Layer can be implemented using the Adapter pattern to translate between different data formats or structures
|
||||
* [Facade](https://java-design-patterns.com/patterns/facade/): The Anti-Corruption Layer can be seen as a specialized form of the Facade pattern that is used to isolate different subsystems
|
||||
* [Gateway](https://java-design-patterns.com/patterns/gateway/): The Anti-Corruption Layer can be used as a Gateway to external systems to provide a unified interface
|
||||
|
||||
## Credits
|
||||
|
||||
* [Domain-Driven Design: Tackling Complexity in the Heart of Software](https://amzn.to/3vptcJz)
|
||||
* [Implementing Domain-Driven Design](https://amzn.to/3ISOSRA)
|
||||
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
|
||||
|
||||
Reference in New Issue
Block a user