Groovy for Domain-Specific Languages


Author(s): Fergal Dearle
ISBN: 184719690X
Published: June 2010
Relevance: 3/5
Readability: 4/5
Overall: 3/5

More Packt Publishing Books Reviews …

Practically this is my first book about Groovy, is compact and something that I like, it is that it let you learn quickly Groovy, if you are a classic Java Developer, reading this book should be easy, furthermore this book has a plus feature, cover about DSL (Domain Specific Languages)

Below my appreciation of the chapters

Chapter 01: Introduction to DSL and Groovy

Here the author share an interesting introduction about JVM and dynamic languages like Ruby and Phyton and of course of Groovy, followed with many valuables pages covering DSL in many aspects. To end the chapter, some Groovy important features are introduced and covered quickly with snippet code.

Therefore you have now a clear idea about Groovy and DSL

Chapter 02: Groovy Quick Start

Here the author start quickly about where to download and how to install Groovy (1.7), after this, how run Groovy but in three ways!, through script engine even covering shebang scripts, Groovy shell and finally with Groovy console; the author for these options shown us an interesting comparison, a Java class and its respective Groovy version for a simple Hello Groovy class, you can see clearly the huge reduction in code on Groovy, futhermore Groovy compiler is covered too quickly.

If you are not comfortable with the previous options you can integrate Groovy with some IDE’s like Netbeans, Eclipse, Intellij Idea and others.

After the previous introduction the author cover the module structure in Groovy with a simple code, not complicated and clearly understandable, from here to forward, many really important features supported by Groovy are covered, valuable theory and code is available in a good amount of pages,some of them are like.

  • assumed imports
  • default visibility, optional semicolon
  • optional parentheses, types and return keyword
  • assertions
  • autoboxing
  • regular expressions
  • method and closured
  • collections
  • operator overloading

Chapter 03: Groovy Closures

An important and interesting chapter, well covered with a good amount of source code, the author after include a solid introduction about what is a closure even including snippet code for a better understanding, we forward with many situations about how closures fit or is applied in some situations, like in collections, as method parameters, even as DSL, forward parameters. Well covered with important code, really easy to understand.

Even more sections we have available, about, calling closures (interesting the three ways offered) and how to work with the closure parameters based in zero, multiple and default options, including curried parameters

To end this chapter, return values and the scope of a closure is included too, even more, the author shown us the behavior with a valuable and important output to see clearly the differences between this, owner and delegate.

Chapter 04: Example DSL: GeeTwitter

Yes, it is based on Twitter, working closely with Twitter4J, with concrete and snippet code the author cover many of the most important features available on Twitter like Tweeting, Direct Messages, Searching and Following, all these topics well covered.

Even with all these features covered with Groovy the author include even more, therefore we have Groovy improvements, then with valuable source code and explanation how to find friends and searching is available, good amount of code, even we havea section about refactoring, well covered!. For this chapter the Groovy Console is used many times for output results purposes.

Be aware this source code would not work anymore since Twitter API has been changed with special control about authentication. Therefore new changes would be mandatory ,but by your own responsibility.

Chapter 05: Power Groovy DSL features

This is a long chapter based on valuable theory and of course source code, many differents topics are covered, like:

  • Named Parameters
  • Builders
  • SwingBuilder
  • Method pointers
  • Meta Object Protocol
  • ExpandoMetaClass

Even many of these sections include its sub sections, recall it is a long chapter.

Chapter 06: Existing Groovy DSLs

Again other long chapter based on valuable material, theory with source code. Starting with Grails Object Relational Mapping (GORM), covering about how to work with Grails, explaining its directory structure, even including source code about Data Source configuration, well covered all these topics, simple and easy to understand, now the author cover GORM, working with a class that represent an Entity, and how it is represented on MySQL, therefore how to work with domain classes is well covered, even more, how to work with relations one-to-one, one-to-many and many-to-many are really well covered with valuable source code with Groovy classes representing these relations, and these same relations reflected on Mysql (even with images) and output results, even more covering inheritance, queries and dynamic finders. A good amount of pages without complicated material is available!.

Now is turn to introduce Gant covering ant and AntBuilder, not many pages available for all this, but well covered.

To end the chapter Behavior Driven Development (BDD) covering quickly with valuable theory and source code the follow topics:

  • GSpec
  • EasyB
  • Spock
  • Blocks

Chapter 07: Building a Builder

An interesting chapter starting quickly with closure method calls which include an image about a Groovy Console showing three different methods definitions, and how to work with them but about nesting calls (nested block structure), something valuable about all this is the addition of a sequence diagram representing these approaches. The previous approach has a problem, therefore the author introduce a solution called Owner_First, after of its explanation a new sequence diagram is introduced based on this new solution, now you can
see the difference.

The next topic is Pretended methods covering quickly, invoke Method and method Missing, with concrete theory and snippet code, same approach for closure delegate.

Now is the turn for Builder Support, a long covering for its hook methods like create Node(…) and others, valuable theory and source code, same approach for Database builder, a lot of pages available for these both topics, really well covered.

Even more, Factory Builder Support is available too, covering with many Factory classes which extends from the Abstract Factory class important methods like:

  • newInstance
  • setParent,setChild
  • isLeaf
  • onNodeCompleted

Again, well covered

Chapter 08: Implementing a Rules DSL

Another long chapter, starting with a valuable theory and source code about Groovy bindings, then the author proceed to apply this bindings in DSL working with closures like built-in methods, repeatable blocks and singleton blocks, some of them are covered quickly and others are expanded about explanation and code.

Bindings and the GORM Data Source DSL is covered shortly, we turn to something interesting about Building a Rewards DSL, really well covered, based on the introduction of a study-case, therefore the author start with the Designing the DSL, some data tables of our study-case are introduced for a better understanding of the situation, then from here to forward value theory and source code is available on many pages, even covering furthermore Handling events, really well covered, even more, to end this chapter the author offer us about Groovy Test Case, valuable source code available.

This is an interesting chapter

Chapter 09: Integrating it all

Starting with a long theory about Mixing and matching Groovy and Java, from here the author cover about POJOs and POGOs, well covered, you understand some differences about how to work with both through executions commands, other sections available is Calling Java from Groovy, Privacy Concerns and Interfaces in Java and Groovy, all these well covered with important and valuable theory and source code.

Now Resolving dependencies is covered, after an interesting theory and snippet code, the author forward covering Dependency injection with Spring, well covered, simple and concrete with important source code.

To end the chapter Groovy Class Loader is available with concrete good material, following with Integrating scripts,Groovy Shell and Groovy Script Engine, all these last sections covered quickly with concrete theory and snippet code.

What I liked:

  • Valuable theory and good amount of source code, you can read the entire book comfortably
  • you learn quickly Groovy, closure and of course DSL
  • Easy to understand 95% the source code through the book
  • Few but important images to complement some ideas

What I disliked:

  • The chapter about Twitter does not work anymore (be fair, these API changes are out of the scope of the author)
  • Even if you learn quickly Groovy, closure and of course DSL, is not deeply (i.e: GORM), you would need read a dedicated chapter or book
  • I have the sensation to need or expected more material about DSL, perhaps more sections or even chapters


I recommend this book if you want get quickly knowledge with Groovy, closure and of course DSL.


More Packt Publishing Books Reviews …