Using decorators to transform data

ID Email Status Date Money
95917 est-vero@voluptua.com EIRMOD 04/24/09 $ 3,370.00
30205 sed-erat@ipsum.com KASD 04/30/09 $ 1,963.00
26057 takimata-diam@sadipscing.com AT 04/11/10 $ 1,051.00
41281 dolor-labore@elitr.com ET 12/19/08 $ 4,626.00
33637 labore-labore@diam.com EST 12/17/08 $ 3,035.00
51961 consetetur-et@dolores.com SED 09/28/09 $ 4,689.00
20906 diam-diam@est.com GUBERGREN 11/03/09 $ 8,944.00
57847 ut-magna@ea.com DIAM 03/13/10 $ 6,837.00
60910 diam-consetetur@no.com JUSTO 05/10/10 $ 8,295.00
53604 sea-labore@At.com ALIQUYAM 11/09/08 $ 4,452.00

A "decorator" is a design pattern where one object provides a layer of functionality by wrapping or "decorating" another object.

Let's assume you have list of business objects that you want to display, and the objects contain properties that don't return native Strings, and you want control over how they get displayed in the list (for example, Dates, money, numbers, etc...). It would be bad form to put this type of formatting code inside your business objects, so instead create a Decorator that formats the data according to your needs.

Notice the following 4 key things (and refer to the TableDecorator javadoc for some of the other details).

  • The Wrapper class must be a subclass of TableDecorator. There is various bootstrapping and API methods that are called inside the TableDecorator class and your class must subclass it for things to work properly (you will get a JspException if your class does not subclass it).
  • Be smart and create your formatters just once in the constructor method - performance will be a lot better...
  • Notice how the getDate() and getMoney() methods overload the return value of your business object contained in the List. They use the TableDecorator.getCurrentRowObject() method to get a handle to the underlying business object, and then format it accordingly.
  • We do not have to overload each of the other business object properties (like getID, getEmail, etc...). The decorator class is called first, but if it doesn't implement the method for the property called, then the underlying business class is called.

The way this works is that a single decorator object is created right before the table tag starts iterating through your List, before it starts processing a particular row, it gives the object for that row to the decorator, then as the various properties getXXX() methods - the decorator will be called first and if the decorator doesn't implement the given property, the method will be called on the original object in your List.

Column Decorators

You can specify decorators that work on individual columns, this would allow you to come up with data specific formatters, and just reuse them rather then coming up with a custom decorator for each table that you want to show a formatted date for. This kind of decorator must implement the ColumnDecorator interface.

ID Email Status Date
95917 est-vero@voluptua.com EIRMOD 04/24/2009 13:51:08
30205 sed-erat@ipsum.com KASD 04/30/2009 13:51:08
26057 takimata-diam@sadipscing.com AT 04/11/2010 13:51:08
41281 dolor-labore@elitr.com ET 12/19/2008 13:51:08
33637 labore-labore@diam.com EST 12/17/2008 13:51:08
51961 consetetur-et@dolores.com SED 09/28/2009 13:51:08
20906 diam-diam@est.com GUBERGREN 11/03/2009 13:51:08
57847 ut-magna@ea.com DIAM 03/13/2010 13:51:08
60910 diam-consetetur@no.com JUSTO 05/10/2010 13:51:08
53604 sea-labore@At.com ALIQUYAM 11/09/2008 13:51:08