anti-corruption-layer/README.md
The Anti-Corruption Layer (ACL) is a crucial design pattern in Java development, particularly for system integration and maintaining data integrity. Implement a façade or adapter layer between different subsystems that don't share the same semantics. It translates between different data formats and systems, ensuring that the integration between systems does not lead to corruption of business logic or data integrity.
Real-world example
This example demonstrates how the Anti-Corruption Layer ensures seamless integration between legacy systems and modern platforms, crucial for maintaining business logic integrity during system migration.
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.
In plain words
The Anti-Corruption Layer design pattern protects a system from the complexities and changes of external systems by providing an intermediary translation layer.
Microsoft's documentation says
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.
Sequence diagram
The ACL design pattern in Java provides an intermediary layer that translates data formats, ensuring that integration between different systems does not lead to data corruption.
Here are 2 shop-ordering systems: Legacy and Modern.
The aforementioned systems have different domain models and have to operate simultaneously. Since they work independently the orders can come either from the Legacy or Modern system. Therefore, the system that receives the legacyOrder needs to check if the legacyOrder is valid and not present in the other system. Then it can place the legacyOrder in its own system.
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:
public class LegacyOrder {
private String id;
private String customer;
private String item;
private String qty;
private String price;
}
Domain model of the Modern system:
public class ModernOrder {
private String id;
private Customer customer;
private Shipment shipment;
private String extra;
}
public class Customer {
private String address;
}
public class Shipment {
private String item;
private String qty;
private String price;
}
Anti-corruption layer:
public class AntiCorruptionLayer {
@Autowired
private ModernShop modernShop;
@Autowired
private LegacyShop legacyShop;
public Optional<LegacyOrder> findOrderInModernSystem(String id) {
return modernShop.findOrder(id).map(o -> /* map to legacyOrder*/);
}
public Optional<ModernOrder> findOrderInLegacySystem(String id) {
return legacyShop.findOrder(id).map(o -> /* map to modernOrder*/);
}
}
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.
public class LegacyShop {
@Autowired
private AntiCorruptionLayer acl;
public void placeOrder(LegacyOrder legacyOrder) throws ShopException {
String id = legacyOrder.getId();
Optional<ModernOrder> orderInModernSystem = acl.findOrderInModernSystem(id);
if (orderInModernSystem.isPresent()) {
// order is already in the modern system
} else {
// place order in the current system
}
}
}
Use this pattern when:
Benefits:
Trade-offs: