Using decorators to transform data

ID Email Status Date Money
48708 dolor-magna@ea.com DOLOR 12/12/09 $ 3,402.00
66692 nonumy-dolore@rebum.com EA 02/08/10 $ 2,182.00
79724 elitr-vero@et.com VOLUPTUA 11/03/09 $ 2,746.00
99664 sed-aliquyam@justo.com AT 11/16/09 $ 9,211.00
17010 eos-voluptua@dolor.com TEMPOR 06/22/09 $ 2,586.00
59411 At-dolore@no.com JUSTO 08/09/10 $ 8,349.00
86446 diam-ea@no.com TAKIMATA 12/29/09 $ 1,944.00
59743 At-gubergren@gubergren.com IPSUM 08/29/10 $ 4,469.00
91508 amet-sit@tempor.com VOLUPTUA 01/10/10 $ 980.00
73693 dolor-Lorem@consetetur.com DOLORES 10/31/09 $ 8,178.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
48708 dolor-magna@ea.com DOLOR 12/12/2009 22:31:19
66692 nonumy-dolore@rebum.com EA 02/08/2010 22:31:19
79724 elitr-vero@et.com VOLUPTUA 11/03/2009 22:31:19
99664 sed-aliquyam@justo.com AT 11/16/2009 22:31:19
17010 eos-voluptua@dolor.com TEMPOR 06/22/2009 22:31:19
59411 At-dolore@no.com JUSTO 08/09/2010 22:31:19
86446 diam-ea@no.com TAKIMATA 12/29/2009 22:31:19
59743 At-gubergren@gubergren.com IPSUM 08/29/2010 22:31:19
91508 amet-sit@tempor.com VOLUPTUA 01/10/2010 22:31:19
73693 dolor-Lorem@consetetur.com DOLORES 10/31/2009 22:31:19