Groovy Goodness: Chaining Traits
Join the DZone community and get the full member experience.
Join For FreeSince Groovy 2.3 we can use traits in our code. We can even chain traits using the so called stackable traits feature. This means a trait can delegate to another trait or we can stop the delegation base based on a condition. Inside the method implementation of a trait we can use super.method
to delegate to the next trait (if available) in the chain. If we don't use super.method
the chain is stopped.
In the following sample code we want to transform a String input value to an output value. We implement the transform
method from theTransformer
interface using different traits. For example the Upper
trait will make the input value upper cased and invokes super.transform
to delegate to the next trait. Then we write new classes that implement the traits. The order of the traits determines the chain. The last declared trait is invoked first then the traits defined from right to left are invoked.
/** * Simple interface with one method to * transform a String value. */ interface Transformer { String transform(String input) } /** Default trait will return the input value unchanged. */ trait DefaultTransformer implements Transformer { String transform(String input) { input } } /** Transform the String value to upper case */ trait Upper implements Transformer { String transform(String input) { super.transform(input.toUpperCase()) } } /** Remove 'mr' from input String value. */ trait Filter implements Transformer { String transform(String input) { super.transform(input - 'mr') } } /** * Simple class uses three traits. The value property get method * returns the transformed value. */ class StringTransformer implements DefaultTransformer, Upper, Filter { String value String getValue() { transform(value) } } // Create StringTransformer instance. def transformer = new StringTransformer(value: 'mrhaki') assert transformer.value == 'HAKI' // Use same traits, but in different order. class OtherStringTransformer implements DefaultTransformer, Filter, Upper { String value String getValue() { transform(value) } } // Create OtherStringTransformer instance. def otherTransformer = new OtherStringTransformer(value: 'mrhaki') // The Filter trait cannot find 'mr', // because the String value is already in // upper case after the Upper trait. assert otherTransformer.value == 'MRHAKI' /** Only chain input values smaller than 5 characters. */ trait SmallFilter implements Transformer { String transform(String input) { if (input.size() < 5) { super.transform(input) } else { '' } } } class SmallStringTransformer implements DefaultTransformer, Upper, SmallFilter { String value String getValue() { transform(value) } } def smallTransformer = new SmallStringTransformer(value: 'mrhaki') assert smallTransformer.value == '' smallTransformer.value = 'haki' assert smallTransformer.value == 'HAKI'
Code written with Groovy 2.3.1.
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How Web3 Is Driving Social and Financial Empowerment
-
Send Email Using Spring Boot (SMTP Integration)
-
RAML vs. OAS: Which Is the Best API Specification for Your Project?
-
Is Podman a Drop-in Replacement for Docker?
Comments