General approach when working with Vaadin


Let’s assume that I clicked menu item for the first time. A new Panel was created. Then I click menu item once again. And now is the question. Shall I create another panel or shall I take reference to the one that was created when I clicked for the first time?




This is one of those situations that depend on your case.
It’s a tradeoff: if you keep a reference to the Panel, it will continue to use memory, but if you release it, it will use more CPU when being recreated (and gc:d for that matter). If you need to fetch some data when creating the Panel, that might be an ‘expensive’ operation, in which case you’d want to keep the initialized Panel around.
Also, if you want to keep the Panel in the same state that it was before (if the user interacted with it), it’s obviously easier to keep it in memory, but if you want to reset it each time, it’s probably a bit easier to recreate it.

So I guess the optimal solution is to choose approach based on panel contents and the intended/probable way of switching between these.

If you really want a general approach you might want to instantiate lazily and release if needed - i.e create the Panel when needed and keep weak references to your ‘cached’ Panels.

Best Regards,