开发者

Best practice for passing configuration to each GUI object

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 开发者_StackOverflow中文版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


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").


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);
      ...
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜