joels builderbuilder in groovy

Published:
Welcome to the dim corner of the library, where fools rush in and angels fear to tread!

This blog post is ancient. If it is technical, the information is likely inaccurate, or at least out of date. If it is non-technical, it’s entirely possible that the relevant facts and my own opinions have changed significantly since it was written.

It is only preserved as part of this site’s permanent historical archive.

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…