R
Rhino
I need a bit of help in improving the design of some of my common code.
Let's say I have a number of preference dialogs, e.g. FooPreferences,
BarPreferences, and BazPreferences. Each of them is a final class that
extends JDialog and each of them uses a component called ImageChooserPanel,
which extends JPanel.
ImageChooserPanel creates a JPanel that shows an image (JPEG, GIF, etc.) and
provides a button so that the user can invoke a chooser so that a different
image can be selected; the new image is then passed back to the preferences
dialog that is showing the ImageChooserPanel so that it can be used in Foo,
Bar, and Baz, the classes that respectively call FooPreferences,
BarPreferences, and BazPreferences.
Now, I want ImageChooserPanel to get some of the information it needs from
the instance of whichever preferences dialog invoked it. For instance, each
preferences dialog will have Logger and Locale instances that it obtained
from Foo, Bar, or Baz and I would like ImageChooserPanel to get this
information from the preferences dialog so that it knows which Logger and
Locale to use.
I realize that I could pass the Logger and Locale as explicit parameters to
the ImageChooserPanel constructor; that is what I am doing now.
What if I want to get the information a different way, from the instance of
the preferences dialog itself? I know I could pass a reference to the
instance directly to ImageChooserPanel, e.g.:
ImageChooserPanel imageChooser = new ImageChooserPanel(this);
where the above code was invoked in FooPreferences, BarPreferences, and
BazPreferences. Assuming that each of those classes had getters to obtain
their Locale and Logger instances, I'd be all set; all I'd need to do was
create three constructors for ImageChooserPanel and then interrogate the
references to the parent component within each constructor, like this:
public ImageChooserPanel(FooPreferences fooPreferences) {
this.Locale = fooPreferences.getLocale();
this.Logger = fooPreferences.getLogger();
}
public ImageChooserPanel(BarPreferences barPreferences) {
this.Locale = barPreferences.getLocale();
this.Logger = barPreferences.getLogger();
}
public ImageChooserPanel(BazPreferences BazPreferences) {
this.Locale = bazPreferences.getLocale();
this.Logger = bazPreferences.getLogger();
}
But what if I had DOZENS of preference dialogs, not just FooPreferences,
BarPreferences, and BazPreferences? How could I generalize the code so that
there was only a single ImageChooserPanel constructor that could invoke the
appropriate getters of the incoming instance regardless of its class?
I'd prefer NOT to have a big 'if' in this single constructor that tried to
determine the class of the incoming reference if at all possible....
I'm guessing that there is a simple and concise way to access the getters
for the specific instance that is being passed to the ImageChooserPanel
constructor but I can't think of it....
Let's say I have a number of preference dialogs, e.g. FooPreferences,
BarPreferences, and BazPreferences. Each of them is a final class that
extends JDialog and each of them uses a component called ImageChooserPanel,
which extends JPanel.
ImageChooserPanel creates a JPanel that shows an image (JPEG, GIF, etc.) and
provides a button so that the user can invoke a chooser so that a different
image can be selected; the new image is then passed back to the preferences
dialog that is showing the ImageChooserPanel so that it can be used in Foo,
Bar, and Baz, the classes that respectively call FooPreferences,
BarPreferences, and BazPreferences.
Now, I want ImageChooserPanel to get some of the information it needs from
the instance of whichever preferences dialog invoked it. For instance, each
preferences dialog will have Logger and Locale instances that it obtained
from Foo, Bar, or Baz and I would like ImageChooserPanel to get this
information from the preferences dialog so that it knows which Logger and
Locale to use.
I realize that I could pass the Logger and Locale as explicit parameters to
the ImageChooserPanel constructor; that is what I am doing now.
What if I want to get the information a different way, from the instance of
the preferences dialog itself? I know I could pass a reference to the
instance directly to ImageChooserPanel, e.g.:
ImageChooserPanel imageChooser = new ImageChooserPanel(this);
where the above code was invoked in FooPreferences, BarPreferences, and
BazPreferences. Assuming that each of those classes had getters to obtain
their Locale and Logger instances, I'd be all set; all I'd need to do was
create three constructors for ImageChooserPanel and then interrogate the
references to the parent component within each constructor, like this:
public ImageChooserPanel(FooPreferences fooPreferences) {
this.Locale = fooPreferences.getLocale();
this.Logger = fooPreferences.getLogger();
}
public ImageChooserPanel(BarPreferences barPreferences) {
this.Locale = barPreferences.getLocale();
this.Logger = barPreferences.getLogger();
}
public ImageChooserPanel(BazPreferences BazPreferences) {
this.Locale = bazPreferences.getLocale();
this.Logger = bazPreferences.getLogger();
}
But what if I had DOZENS of preference dialogs, not just FooPreferences,
BarPreferences, and BazPreferences? How could I generalize the code so that
there was only a single ImageChooserPanel constructor that could invoke the
appropriate getters of the incoming instance regardless of its class?
I'd prefer NOT to have a big 'if' in this single constructor that tried to
determine the class of the incoming reference if at all possible....
I'm guessing that there is a simple and concise way to access the getters
for the specific instance that is being passed to the ImageChooserPanel
constructor but I can't think of it....