Introduction: Groovy's concise syntax frees developers from the typical Java™ architecture that requires code compilation but does not help express what the program really wants to achieve. In this revival of the Groovy series, groovy developer and special columnist J. Scott Hickey takes you through a series of comparisons between regular Java code and Groovy Code, showing how this exciting language frees you up so you can focus on coding Important aspect.
Typically, programmers turn to programming languages such as Groovy to build fast utilities, quickly write test code, and even create components that make up large Java applications, and Groovy has the ability to reduce the number of traditional Java-based systems that are inherently Redundancy and reduce its complexity. Groovy's concise and flexible syntax frees developers from the typical Java architecture that requires code compilation but does not help express what the program really wants to achieve. Not only that, Groovy's easy type reduces the complexity of the code by reducing some interfaces and superclass, both of which are required by regular Java applications to support common behavior between different concrete types.
To illustrate how Groovy reduces the useless data involved in Java applications, I'll use the sample code in the Spring:a Developer ' notebook (see Resources) of Bruce Tate and Justin Ghetland, the book Describes how to use Spring for control inversion. Whenever I review a Java sample, I compare it to the corresponding Groovy source code that implements the same functionality, and you will soon discover that Groovy makes application code much clearer by reducing the different aspects of Java programming, which are redundant and unnecessarily passing the application's behavior.
The Sound of Groovy
In the first chapter of Bruce and Justin's book, A simple bicycle store application was created, containing four classes. First, I'll show you a simple JavaBean class called Bike, which represents an inventory of bicycles. Then, I'll examine the type of bike store called Rentabike. It contains a Bike set. There is also a class named Commandlineview that displays a list of bicycles, which is dependent on the Rentabike type. Finally, there is a class that integrates these parts to create a working application that uses Spring to pass the Commandlineview class, which is fully configured with the Rentabike type--eliminating complex hard coding.
Deactivate javabean!
In Listing 1, a class that represents bicycles is implemented as a simple JavaBean in regular Java code, which is a typical of hundreds of thousands of classes that Java developers may have written. Generally speaking, JavaBean is nothing special-its attributes are declared private and accessible through public getter and setter.
Listing 1. Bike JavaBean in Java code
import Java.math.BigDecimal;
public class Bike {
private String manufacturer;
Private String model;
Private int frame;
Private String Serialno;
Private double weight;
Private String status;
Private BigDecimal cost;
Public Bike (string manufacturer, string model, int frame,
string serialno, double weight, string status) { br> this.manufacturer = manufacturer;
This.model = model;
This.frame = frame;
This.serialno = Serialno;
This.weight = weight;
This.status = status;
}
Public String toString () {
return "Com.springbook.Bike:" +
"Manufacturer--" + M Anufacturer +
"\n:model--" + model +
"\n:frame--" + frame +
"\n:serial No--"+ Serialno +
" \n:weight--"+ Weight +
" \n:status-"+ Status +
". \ n";
Public String Getmanufacturer () {REturn manufacturer;
public void Setmanufacturer (String manufacturer) {
This.manufacturer = manufacturer;
Public String Getmodel () {return model;}
public void Setmodel (String model) {This.model = model;}
public int GetFrame () {return frame;}
public void Setframe (int frame) {this.frame = frame;}
Public String Getserialno () {return serialno;}
public void Setserialno (String serialno) {this.serialno = Serialno;}
Public Double Getweight () {return weight;}
public void Setweight (double weight) {this.weight = weight;}
Public String GetStatus () {return status;}
public void SetStatus (String status) {this.status = status;}
Public BigDecimal Getcost () {return cost;}
public void Setcost (BigDecimal cost) {
This.cost = Cost.setscale (3,bigdecimal.round_half_up);
}
}