I asked such a question. Do I try to design GUI correctly? Now I am making an application on Swing. There is one JFrame , setContentPane of the same JPanel size is exposed in it. Then another JPanel is loaded onto this JPanel by clicking on the button, then certain actions, the last JPanel disappears, a new one appears, and so on. Imagine the "Next" button. Now I do this: I pass the previous panel to the constructor, then, when I did not need it, I called the method "push and load something else" and so on. I now speak only about the View level.

I was advised today, and why not make the loadable panels Singleton static? This is a good practice, this is the best practice, this is not a good practice? If anyone can, a couple of arguments ...

Thank!

  • probably good practice, it turns out that all views are remembered and are not re-created + control their count - Gorets

1 answer 1

If there are a lot of such elements JPanel , JButton , etc., then you need to think about their caching.

The cache is usually implemented in the Singleton pattern — that is, a certain static class (or static class object) containing a finite list of items to be cached. Has methods to add to the cache and issue from the cache. The cache has a small tredik inside, which with some frequency checks when the last time an object was used. If it has not been used for a long time, then it "kills" it. Accordingly, when retrieving from the cache, if there is no object in the cache through the designer, we create and issue it.

In general, it is such a kind of best practical - it allows you to find a reasonable compromise between the preservation in memory of all objects (according to its pollution) and the speed of issue.

  • No, there are not many interface elements, and if in fact? Thank! - JDev
  • Well then do it in the pattern Singleton - this is normal - Barmaley
  • @Barmaley, cache? eg? - Gorets
  • @Gorets - did not understand the question - Barmaley
  • An example of such a cache is possible? those. implementation of this moment "The cache has a small tredik inside, which with some frequency checks for the last time an object was used. If it has not been used for a long time, it" kills "it. Accordingly, when retrieving from the cache, if there is no object in the cache through the constructor create and issue. " never met such a thing before - Gorets 6:24 pm