views:

47

answers:

2

Hi,

I am writing an application, where I do have few different windows implemented, where each window is a separate class. Now I need somehow to pass a single configuration object to all of them. My GUI is designed in way, where I have one main window, which may create some child windows of its own, and these child windows can have their own childs (so there is no possibility to create all windows in initialization part and feed the config object to all of them from the very beginning)...

What would be best practice for sharing this configuration object between them? Always passing via constructor or maybe making it somewhere as final public static and let each window object to access it when needed?

Thanks

A: 

In this case I would prefer your second approach. Something like:

Configuration.getCurrentConfig ();

I've also seen a solution of the similar problem by subclassing JFrame and further inheritance:

public abstract class BasicFrame extends JFrame {

   protected BasicFrame (Map<String, String> configParams) {
      ...
   }
}

public class SomeFrame extends BasicFrame {

   public SomeFrame (Map<String, String> configParams, String title) {
      super (configParams);
      ...
   }
}
Roman
+3  A: 

Always pass through the constructor. Avoid mutable statics.

It sounds like you are in the old school extend everything. If a class need not be extended (generally, if you are not overriding any methods), then don't extend it. "Prefer composition to inheritance."

OTOH, a big "knows about everything; everything knows about it" context class is not good either. Keeps things small, with each class doing one thing (for some suitable definition of "one thing").

Tom Hawtin - tackline
What about FacesContext?
Roman
do as Tom says!
fabrizioM