Transcript Basic Scala
Groovy
Overview
First released in 2004
Dynamically compiled to JVM bytecode
Most Java code is syntactically valid Groovy
Unlike Java, Groovy can be used as a scripting language
2
Groovy vs. Java
3
Domain Specific Languages
A DSL is a computer language that’s targeted to a particular kind
of problem, rather than a general purpose language that’s aimed
at any kind of software problem
Three main Groovy features make it easy to write DSL’s:
Method Interception - every class has an invokeMethod() method that can
be overridden and will be called first when you call a non-existent method
on an object of the class.
Dynamic Invocation - Groovy can call methods and get and set
properties/fields of objects dynamically.
Closures + Delegates – A closure in Groovy is an open, anonymous, block
of code that can take arguments, return a value, and be assigned to a
variable. The context in which a closure is defined is stored in a closure’s
delegate property and can be modified dynamically.
4
Problem
Given the Customer class to the left, we want to create a
Validator class that will validate the fields based on the
constraints closure in the Customer class
The Validator class will be instantiated and the validate() method
will be called as seen on the right
5
Solution: Groovy to the rescue
6
Further Reading
Closures: http://groovy-lang.org/closures.html
DSL example in more detail:
http://www.artima.com/weblogs/viewpost.jsp?thread=291467
Another great DSL example: https://dzone.com/articles/groovydsl-simple-example
More about DSL’s:
http://www.martinfowler.com/bliki/DomainSpecificLanguage.ht
ml
7