Book Image

Groovy for Domain-Specific Languages, Second Edition

By : Fergal Dearle
Book Image

Groovy for Domain-Specific Languages, Second Edition

By: Fergal Dearle

Overview of this book

Table of Contents (20 chapters)
Groovy for Domain-specific Languages Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introduction to DSLs and Groovy
Index

The builder code structure


The real beauty of Groovy's builder paradigm is the way in which it maps the naturally nested block structure of the language to the construction process.

The process of defining parent-child relationships between objects through nested code blocks is well-established through other markup languages, such as XML and HTML.

The transition from writing XML or HTML to the GroovyMarkup equivalent is an easy one. To make use of a builder, we don't need to have an intimate understanding of the Groovy MOP or of how the builder is implemented. We just need to know how to write the builder code so that it conforms to the correct language semantics. The code structure of the builder pattern relies on just a few Groovy language features:

  • Closure method calls: The distinctively nested block structure in the builder pattern is facilitated by Groovy's special handling of closures when they are passed as method parameters. This allows the closure block to be declared inline after...