bin^2

bin^2

discord server
twitter

Simplify anti-corrosion layer design using Kotlin

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

image
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
}

image
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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.