Groovy is a dynamic, dynamically-typed, object-oriented scripting programing language, which takes inspiration from Ruby, Smalltalk or Python, and combines all the powerful features with Java-like syntax.
Re-define accessors Read-only properties Combinations of parameters in constructors Access without calling accessors
Methods become first-class citizens Represent a class with a sole method
Further simplification for closures with single parameters
Over collections you can iterate using methods like e.g. each(), upto() or step(), which take closures as parameters and invoke the closure on each element in the collection. The closures can of course interact with the rest of the code. Don’t get confused by missing parentheses around the closures, that’s just a syntax simplification. You could include them.
Groovy is also well suited for testing. You can test both Java and Groovy code, run the tests from Ant, Maven or your IDE and Groovy has integrated quiete a few nice features that help writing tests.
Groovy is ready to help you extend your Java applications into several new domains.
With scripting you can add code to your application at runtime. For example, you can supply a Runnable implementation in a file or a string and your application will parse it, compile and use it as if it was part of your program from the beginning.
Build object hierarchies or hierarchical documents The code resembles the generated document
It is valid Groovy code You can embed control logic
Which aproach is more object-oriented?
DSL are special purpose programming languages targeted at a particular domain or a framework to ease communication between domain experts. The languages don’t need to do more that what they have been designed for. Think of SQL or HTML as good examples. They define their own grammar, they need a parser and they nicely focus on their primary task and nothing else. These were example of external DSLs, as they have their own parser. For more ad-hoc DSLs internal DSLs are more practical. They are embedded into a host language and reuse its grammar and parser. The definition and integration of such languages is much faster then for external ones. However, you’re limited by the host language, which is why there are not many DSLs in Java. But in Groovy the situation is much better.
We are all domain experts for dates.
Some of you are domain experts for Hibernate query criteria.
Here’s my own DSL for my accounting application. Look at the difference in writing code.
You saw the powerful syntax enhancements that Groovy has, we looked into builders, scripting and DSL creation. You should be able to start writing your first Groovy applications now.
Groovy for Java experts V á clav Pech Software Developer and Product Evangelist JetBrains, Inc.
DSL <ul><li>Limited purpose language </li></ul><ul><li>Targeted to a particular domain </li></ul><ul><li>Friendlier API to a framework </li></ul><ul><ul><li>External </li></ul></ul><ul><ul><ul><li>SQL, HTML, CSS, … </li></ul></ul></ul><ul><ul><li>Internal </li></ul></ul>