July 8, 2008 – 12:07 am
In my previous post I talked about dynamically adding behavior to Groovy classes using either the ExpandoMetaClass or Categories. These techniques are especially useful if you know which methods you would like to add to your classes prior to actually writing any code. But what if you don’t know which methods you will need before code writing time? What if you want to allow yourself the flexibility to call methods arbitrarily without defining their implementation beforehand?
To the average Java programmer (myself included), the idea of generating methods on the fly might seem bizarre at first, but Groovy’s built in method interception capabilities allow programmers to easily realize this type of functionality in their Groovy code. In Groovy Metaprogramming - Adding Behavior Dynamically, I discussed how to add a method to the java.math.BigDecimal class to allow for the conversion of U.S. Dollars (USD) into Euros (EUR). What if, however, I wanted to convert from USD to British Pounds (GBP) or to Japanese Yen (JPY)? I could certainly write a method for each of these conversions, but what if there was a better, cleaner, and more flexible way to build in this functionality?
Read the rest
Posted in Programming | 3 Comments »