Monday, February 12, 2018

Groovy: Comparison to Programming Paradigms

Groovy is derived from Java and as such follows many of the object-oriented design paradigms, however its scripting nature and other elements allows it to implement elements of other programming paradigms.  This section compares Groovy to each of the key paradigms.

Imperative Programming

Unlike Java where everything is an object (with the exception of primitives), Groovy offers a scripting framework that allows for a more imperative method of programming.  A Groovy script operates as a set of variables containing data that can be acted upon via functions.  These functions perform operations and can return a result.  This result is stored can be stored in a variable in the code.
Unlike a true imperative language, like C, Groovy still maintains two key elements of object-oriented programming:
  • Unlike C, a variable in Groovy can store not only a value but also a reference to an object.
  • Under the covers, Groovy converts all scripts to a Java object in order to be executed by the JVM.  This conversion is transparent to the developer, but it is an abstraction that is described in greater detail in the next section.

Object-Oriented Paradigm

Groovy is an object-oriented language and follows all key OO paradigms including:
  • Everything is an object
  • Classes and subclasses for polymorphism
  • Inheritance
  • Inclusion polymorphism
As noted in the imperative paradigm section, Groovy does offer a scripting semantic that appears to implement the imperative paradigm.  However, at compilation time, the script itself is converted into a proper Java object with the globals, functions and other elements handled within the class.  Below is a sample Groovy script:
// A sample Groovy script
a = 1
def b = 2
 
def doSomething()
{
    def c = "Foo"
    def d = a
}
The following code example is the same script cross-compiled into Java by the Intelli-J integrated development environment.
// A cross compiled Groovy script
public class Sample extends Script {
    // Groovy-specific constructors omitted
    public static void main(String[] args) {
        new Sample(new Binding(args)).run();
    }
    public Object run() {
        setProperty("a", 1);
        Integer b = 1;
        return null;
    }
    public void doSomething() {
        String c = "Foo";
        Object d = this.getBinding().getProperty("a");
    }
}
Global variables are managed through a Bindings object which manages the scope and access within created methods.  Any functions created in the script are promoted to class-level methods.  All other elements are managed using normal object-oriented paradigms.

Concurrent Paradigm

Groovy implements elements of current programming.  The underlying processing model allows for:
  • Creating and managing multiple threads to process in a parallel or interleaved fashion
  • Event management
  • Mutual exclusion via synchronization and atomic object classes
  • Admission control via Semaphore

Functional Programming

Groovy implements elements of the functional programming paradigm through the implementation of closures.  Closures allow for defining a function as a variable and then using that function in various ways, including list processing.

Scripting

Groovy implements key elements of the scripting paradigm.  In fact, one of the primary goals when Groovy was created was to offer a scripting language like Python that targeted the Java Virtual Machine.  A developer can create a compact script of commands and variables that can be executed in an interactive fashion either via the groovy command line tool or in the Groovy shell.
It implements script-like variable binding and scope by offering script-level globals and variable definition within the script itself.  Variables are optionally typed, adding a def keyword that defines a variable as untyped.  Groovy offers the ability to scope variables and functions within packages and offers access to the underlying Java packaging system.
Data abstraction is accomplished using packages as well as through Java classes and objects.

No comments: