bin^2

bin^2

discord server
twitter

Simplify Anti-Corruption Layer Design Using Kotlin

Simplifying Anti-Corrosion Layer Design Using Kotlin#

Kotlin Reference#

[[How I Use Xlog - PlantUML Rendering]]

Java Anti-Corrosion Layer#

package Anti-Corrosion-Layer {
	component Interface {
		interface InterfaceDefinition
	}
	component Implementation {
		class InterfaceImplementation
	}
	InterfaceImplementation --|> InterfaceDefinition
}
package Application {
	component Functionality {
	}
	
	component App {
	}
}
Functionality --> Interface
App --> Functionality
App --> Implementation

image
To prevent direct usage of interface implementation by functionality components, the interface implementation needs to be separated into a standalone implementation component. Functionality components should only depend on the interface component.

Kotlin Anti-Corrosion Layer#

package Anti-Corrosion-Layer {
	interface InterfaceDefinition
	class InterfaceImplementation
	note left: Package visibility
	InterfaceImplementation --|> InterfaceDefinition
}
package Application {
	component App {
	}
	component Functionality {
	}
	App --> Functionality
	Functionality --> Anti-Corrosion-Layer
}

image
Since Kotlin's package visibility can prevent functionality components from directly using interface implementations, there is no need to separate the interface implementation into a separate component when using Kotlin. This reduces the number of components overall, 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 interface implementations.

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