Groovier: Implement interface in Groovy using closure
Groovy’s closure
can be used to implement the interfaces with pretty ease. This can come handy when creating scaffolding of code or creating mocks for testing.
Single method interface with same implementation
interface AwesomeInterface {
void x(String x)
void y()
}
def interfaceImpl =
{ String str -> println("Implementation of methods of interface with value: ${str}") } as AwesomeInterface
interfaceImpl.x("Awesome")
interfaceImpl.y() // will call same implementation with null arguments
Output
Implementation of methods of interface with value: Awesome
Implementation of methods of interface with value: null
Multiple methods implementation in an interface
interface AwesomeInterface {
void x(String x)
void y()
}
// Use Map as method name as key and Closure as an argument with the implementation
def interfaceImpl = [
x: { String str -> println("Implementation method x() of interface with argument: ${str}") },
y: { println("Implementation of method y() of interface") }
] as AwesomeInterface
interfaceImpl.x("Awesome")
interfaceImpl.y()
Output
Implementation method x() of interface with argument: Awesome
Implementation of method y() of interface