Monday, May 05, 2008

ObjectGraphBuilder - another grooviest builder in Groovy

One of the nicest features of Groovy is Builders. It is a powerful concept and creating your own Domain Specific Languages (DSLs) using this concept is a breeze. Groovy comes with several handy builders like HTML, XML, Swing etc. ObjectGraphBuilder has been a new addition to this family. Just out-of-the-box, this builder comes in very handy and useful for creating complex Object Graphs. Especially, if you have followed javabean conventions, the default strategies implemented in this builder are all good for you and you are ready to go with it. But, what if you have not followed javabean conventions? What if you have followed for some of your objects but not for all? In those situations, you will have the option to override some or all of the default implementations of strategies for which you have violated (rather deviated from) javabean conventions.

Lets taks an example Object Graph that covers most of deviations and see how simple it is to override default implementation strategies to meet those situations.

Lets take the same example from the Groovy doumentation but deviate from javabean conventions for this exercise:

package com.acme class Company { String name Address address List employees = [] } class Address { String line1 String line2 int zip String state } class Employee { String name int employeeId Address address Company company } package com.acme class Company { String name Address address List currentEmployees = [] //deviated from javabean conventions } class Address { String line1 String line2 int zip String state } class Employee { String name int employeeId int id //we have an id property for our own reasons of use Address homeAddress //deviated from javabean conventions String address //not an Address indicating the place but used to specify the form of addressing an Employee (e.g. Mr., Mrs., Ms. etc) Company company }


Since I have deviated from conventions, my ObjectGraphBuilder also needs a deviation from default strategy implementations as well. Makes sese.

No comments:

Post a Comment