How to Write POGOs

In the Java programming language a Java bean is often referred to as a "Plain Old Java Object", or "POJO". The equivalent concept in Groovy is referred to as a "POGO", or "Plain Old Groovy Object". It is the equivalent of the Java bean. Therefore, the POGO permits you to store properties for a business entity such as an order, a customer, a vendor or a person. In this lesson we will focus on a common application of POGOs by creating a Person bean.

To learn how to use POGOs in Groovy follow these 5 steps:

  1. First, you will create the Person POGO. Open your text editor and type in the following lines of Groovy code:
    // Person POGO (Plain Old Groovy Object)
    class Person {
    	String fullName
    	String emailAddress	
    	public String toString() {
    		"Name: $fullName Email: $emailAddress"
    Very straightforward, just like a Java bean. If you're a Java developer, however, you may well be asking, "Where are the getters and setters?" The answer is Groovy provides those for you! Therefore, explicit getXXX and setXXX (where "XXX" is the property name) are not required in Groovy but may be provided in those cases where you want to tailor getter or setter implementation. Note that a toString method has been provided as a convenience for object state display.
  2. Save your file as Person.groovy.
  3. Open a command prompt and navigate to the directory containing your new Groovy POGO. Then type in the command to compile your source:
    Compile Groovy POGO
  4. Now, let's create a tester in the same directory. Open your text editor and type in the following lines of Groovy code:
    import Person
    def person = new Person( fullName: "Stephen Withrow", emailAddress: "")
    println ("Person data: \n $person")
    println ("Our new person's name is $person.fullName")
    println ("Updated person data: \n $person")
    Note that we are required to import the Person class. The program instantiates a Person object using the new keyword. The Person constructor is called and parameter data is provided using the parameter names defined in the POGO. This constructor is provided by Groovy and is not a part of the Person source that you created in the first step of this topic. The program displays the full object state (i.e., the values of full name and email address) and also the fullName property only. The final step is to change the email address and then display the object state again to verify the email address was successfully updated.
  5. In the command prompt, type in the command to interpret and run your script:
    Run pogo script
    The program output verifies the correct object state before and after the email address update.
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.