本文由 AI 分析生成
建立時間: 2026-03-28 來源: https://java-design-patterns.com/patterns/anti-corruption-layer/
Summary
A reference entry on the Anti-Corruption Layer (ACL) pattern from java-design-patterns.com, covering its intent, real-world analogues, programmatic Java example, and when to use/avoid it. ACL is an Eric Evans DDD pattern that places a translation/adapter layer between subsystems with incompatible semantics to prevent external models from “corrupting” internal domain models.
java-design-patterns.com 的 ACL 模式參考頁,涵蓋意圖、實際案例、Java 程式範例及適用時機。ACL 是 Eric Evans DDD 模式,在語義不相容的子系統之間放置翻譯/適配層,防止外部模型「汙染」內部領域模型。
Key Points
- Core intent: implement a façade or adapter between subsystems with different semantics — translate requests in both directions without exposing internal models to external system’s concepts
- Origin: Eric Evans’ Domain-Driven Design (DDD); ACL is one of the strategic patterns for managing relationships between bounded contexts
- Real-world use case: retail company migrating from legacy inventory system to modern platform — ACL translates requests from new system to legacy format, retrieves data, translates response back. New system never needs to understand legacy data structures
- Java implementation: typically a service class that wraps the legacy/external client, maps DTOs between domain models, and exposes only the internal model to callers
- Also known as: Interface Layer, Translation Layer
- When to avoid: when both systems share the same domain model and semantics — ACL adds overhead with no benefit
Insights
The ACL pattern is often described as a “gift to future developers” — it makes the boundary of what your system owns explicit. Without it, legacy concepts and data shapes leak into the domain model, making it progressively harder to migrate or refactor either system independently.
The most common failure mode in legacy integration is skipping the ACL and directly mapping external data structures into internal domain objects. This creates hidden coupling: when the external system changes its API, the internal domain model has to change too. ACL breaks that coupling.
ACL is particularly valuable during incremental migrations (strangler fig pattern): as you progressively replace legacy functionality, the ACL remains stable even as both sides change, because translations are encapsulated in one place.
Connections
Raw Excerpt
The Anti-Corruption Layer design pattern protects a system from the complexities and changes of external systems by providing an intermediary translation layer. This ensures that the application’s design is not limited by dependencies on outside subsystems.