joels builderbuilder in groovy

Joel Neely started a series of posts over the weekend detailing a proposed exploration of what Functional Programming means “to me as a practicing OO software developer?” The task at hand is to look at the generation of Data Transfer Objects which include a static inner class that functions as a builder. Since I'm exploring both Clojure and Scala right now, Joel has asked me to play along.

After reading the task definition today, I couldn't resist taking a crack at the problem in Groovy, especially since tackling a Builder DSL is not something I've gone after before.

So, here's how I'll use my DTOBuilder to produce the output Joel described:

def bldr = new DtoBuilder()

println bldr.build {
	packageName 'edu.bogusu.registration'
	name 'Student'
	field(name:'id', type:'String')
	field(name:'firstName', type:'String')
	field(name:'lastName', type:'String')
	field(name:'hoursEarned', type:'int')
	field(name:'gpa', type:'float')
}

Pretty concise, eh? Check out the implementation of it at GitHub. I'll be posting all of my code related to this series at this location (and hopefully Joel will join in as well).

One thing you'll notice is that I'm still operating primarily in OO style. My next task is to refactor this code, still written in Groovy, but using as much functional-style as I can squeeze out of the language. Until next time…

Avatar
Matt Stine
Executive Director, Architecture

My research interests lean/agile software development methodologies, DevOps, architectural principles/patterns/practices, and programming paradigms.