How builders work

Earlier, when we looked at the MarkupBuilder code, the unfamiliar syntax must have seemed strange. Now that we have an understanding of how the MOP and pretended methods work, let's take a quick look again at some builder code and see if we can figure out what might be happening. MarkupBuilder is derived from the BuilderSupport class. When describing how MarkupBuilder works, I won't make a distinction between BuilderSupport and MarkupBuilder. Most of the mechanisms described here are in fact implemented by BuilderSupport and are shared with other Builder classes:

def customers = builder.customers { customer(id:1001) { name(firstName:"Fred",surname:"Flintstone") address(street:"1 Rock Road",city:"Bedrock") } customer(id:1002) { ...

Get Groovy for Domain-specific Languages - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.