Separation of concernsSubmitted on 18 February 2012
Last week, we quickly created a Delete generated column in a table.
Although this is enough to attain our objective, two nested anonymous classes puts a strain on maintenance costs. Moreover, behavior code (the deletion) is interwoven with GUI code (column).
Correcting these mistakes can be achieved through the creation of two top-level classes, one for the deletion behavior, and the other for the column. We have to find a way to pass the data container and the item id to the behavior class: the most obvious way to do so is to change the latter’s structure to store both like so:
This design is much more decoupled and maintenance friendly than the previous one, but we can do better. The click listener’s structure is at present tightly coupled to the parameters it needs to access. It would be nice to remove this coupling. Fortunately, Vaadin components can store data on their own. Let’s rely on this feature to clean our design.
First, create a placeholder for the container and the item id:
Then, we remove all references to both container and item id from the click listener and use the Vaadin way:
Notice how we get the reference to the button in the event, it’s enough!
Finally, we have to pass the data to the button in the column generator:
This new design is much more modularized: for application that go beyond prototypes, this approach should be favored over the previous one.