How to Write POGOs

  • google plus

In Brief...

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.

Instructions

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: "sfw@coolMail.com")
    println ("Person data: \n $person")
    println ("Our new person's name is $person.fullName")
    person.emailAddress="stephen@coolestMail.com"
    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' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss