Simplifying Anti-Corruption Layer Design Using Kotlin#
Kotlin Reference#
[[How I Use Xlog - PlantUML Rendering]]
Java Anti-Corruption Layer#
package Anti-Corruption-Layer {
component Interface {
interface InterfaceDefinition
}
component Implementation {
class InterfaceImplementation
}
InterfaceImplementation --|> InterfaceDefinition
}
package Application {
component Feature {
}
component App {
}
}
Feature --> Interface
App --> Feature
App --> Implementation
To prevent the Feature component from directly using the Interface Implementation, the Interface Implementation needs to be separated into a standalone Implementation component. The Feature component should only depend on the Interface component.
Kotlin Anti-Corruption Layer#
package Anti-Corruption-Layer {
interface InterfaceDefinition
class InterfaceImplementation
note left: Package visibility
InterfaceImplementation --|> InterfaceDefinition
}
package Application {
component App {
}
component Feature {
}
App --> Feature
Feature --> Anti-Corruption-Layer
}
Since Kotlin's package visibility can prevent the Feature component from directly using the Interface Implementation, there is no need to separate the Interface Implementation into a separate component when using Kotlin. This reduces the overall number of components, making it easier to understand and maintain.
Note#
Kotlin's package visibility only applies to Kotlin, so when Java uses a library written in Kotlin, it cannot prevent Java from directly using the Interface Implementation.