this is more a design question. Suppose you have many beans A, B, C…. Then you have a class that creates, say, tables out of beans. You need something that tells this class which properties of those beans to display, what names give to the columns and other rendering properties. I don’t want to hardwire this in the class for each bean class, so I was thinking of giving the beans a common interface like TableAdapterProvider, that would come out with a class that gives all instructions to the rendering class. The problem is that I can’t make an interface method static in Java. And this doesn’t seem very clean for a number of reasons… for example to create an empty table I have to allocate a bean and then call the interface method TableAdapterProvider to retrieve the “rendering” class.
I thought of creating a factory like TableAdapterFactory(Class beanClass) that returns the correct TableAdapter for each bean class without instanting any. Anyway I have to update this factory for every bean I create.
Another solution could be use special naming conventions, for example if a bean name is Apple, then AppleTableRenderer will be the renderer. I can scan a package in search of this when the TableRenderer starts so the associations are done automatically.
Do you know of a better pattern? What do you think of my approaches?
For the future, is there a good book with recipes like these to use in web apps?
Seems to me that any bean (A, B, C..) that needs to be displayed as a HTML table (by some kind of
TableRenderer) should implement an interface that provides the renderer with the required information. The likely interface methods would get the columns headers and the table data. Maybe also one for table style, but that is a bit subjective.With this in mind you might have;
It’s then pretty trivial to write your
TableRenderer;Note that instead of writing a
TableRendererclass you might want to use the JSTL<c:forEach>syntax, which is a more standard approach.