• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reuse component instances vs creating new ones.

 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I've got a question regarding Components.
Let's assume you've got a cardlayout with panels in it. In fact it is a wizard like like structure.
Now this wizard has many components per card and it is reused very often to display and edit various data.
Question now would be if it is better to reuse already created instances of those panels and reset the displayed data to the new values or just to create new objects which already have those entries...
Its just the values which change not the components itself.

I personally think it would be good to have a pool of instances. If they are created and not already in use, use those, if they are not create a new instance.

Is this a good way or is there something i need to be aware of?
Thanks
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO it's not really important whether you choose to retain and reuse instances or construct them when needed, so long as you make sure not to hang on to references you no longer have use for, so they are eligible for GC.

If I ever decided to retain a GUI class instance for reuse, I would separate the data model from the view, and set a new model to (re)set all data fields.
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your opinion. It helped a lot.
If there are any more advices either pro or con, please let me know.
For now I will try to reuse GUI instances when possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!