430 likes | 594 Views
An agile dynamic language for the Java Platform. Overview. JSR-241, aims to provide scripting language for Java developers Community driven, open source Compiles to Java byte code, runs inside of the JVM Written in Java and Groovy. Closures.
E N D
Overview • JSR-241, aims to provide scripting language for Java developers • Community driven, open source • Compiles to Java byte code, runs inside of the JVM • Written in Java and Groovy
Closures • Allows one piece of code to execute another piece of code that has been defined elsewhere • Can be done in Java with the Method-Object Pattern • Much cleaner in Groovy
Groovy’s Type System • Everything is an object, there are no primitive types • Allows the programmer to choose between static and dynamic types • Type safe
Duck Typing • Objects can be polymorphic without being related by a common base class or interface
Operator Overriding • Known to C++ programmers as operator overloading • Java provides no support for this • Can be done in Groovy, implemented based on the Value Object Pattern
Anonymous Inner Classes • Familiar to experienced Java developers • No support for anonymous inner classes • There exists a way to do this in Groovy with Maps
Multi-Threading • Disappointing, not much of an improvement above Java • Ability to use closures as opposed to implementing the Runnable interface or extending the Thread class and implementing the run() method • There is a problems with synchronized blocks in closures
Master-Slave Example • For the Master Process a single instance of a closure definition can be used • For the Slave Processes a method that returns a new Closure object is used so that the closure can use variables bound at the time it is instantiated and not the time it is first used
Design Patterns in Groovy • Many patterns are easier to use, program, and read than their Java counterparts • We can thank Closures and Dynamic Typing for this
Builder Pattern • Separates the construction of a complex object from its representation • Hides the internal representation of the product from the client
GPath • Path expression language integrated into Groovy
References • http://www.manning.com/koenig/ • http://www.jroller.com/aalmiray/entry/anonymous_classes_in_groovy • http://groovy.codehaus.org/ • http://c2.com/cgi/wiki?ValueObject • https://gsp.dev.java.net/ • https://groovlet-examples.dev.java.net/