Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
[ 139 ]

Power Groovy DSL Features


In this chapter, we will cover some more advanced Groovy features. Coincidentally,
these are also the features that, along with closures, allow us to extend and
manipulate the language in order to create DSLs. We will cover a lot of ground
in this chapter, including the following important features:



  • Named parameters: To begin, we will look at this simple but effective
    feature, and see how maps passed as parameters act as named parameters
    to a method.

  • Command chains: We will cover how, when chaining methods together, we
    can omit the dot notation to give us a simple command chain DSL pattern.

  • Builders: We will cover how to use Groovy builders to rapidly construct
    anything from web pages and XML to Swing UIs. While looking at Groovy
    builders, we will also introduce the native Groovy support for tree-based
    DOM structures, by looking at the built-in GPath operators in the
    Groovy language.

  • SwingBuilder: We will add a quick and simple UI to our Twitter DSL,
    by using the SwingBuilder class.

  • Method pointers: We will cover method pointers as a useful way to
    create aliases.

  • Meta Object Protocol: We will cover the inner workings of Groovy's Meta
    Object Protocol (MOP).

  • How builders work: Once we have covered the concepts behind the MOP,
    we will revisit Groovy builders to understand how they are implemented
    using features from the MOP.

  • ExpandoMetaClass: Finally, we will take a look at ExpandoMetaClass,
    which is one of the most interesting Groovy classes as it provides the keys to
    dynamically change the behavior of any existing class, including Java classes,
    on the fly.

Free download pdf