Posted By:
John_Kostaras
Posted On:
Wednesday, July 31, 2002 04:20 AM
I have the following problem with factory pattern. I need to decide which one of 3 windows to choose in my application when the application starts. The two of these windows are of type JWindow and the third of type JFrame. The common class of JWindow and JFrame is Window. So the WindowFactory I use has to return an object of type java.awt.Window. However, I cannot extend java.awt.Window to add common classes of my two types of classes (let name them MyJWindow & MyJFrame respectively) as java.awt.Window's constructors are not public. Hence, the solution is to use an association of MyJWindow & MyJFrame to another class (association instead of inheritance as most dp books say). I thus have t
More>>
I have the following problem with factory pattern.
I need to decide which one of 3 windows to choose in my application when the application starts. The two of these windows are of type JWindow and the third of type JFrame. The common class of JWindow and JFrame is Window. So the WindowFactory I use has to return an object of type java.awt.Window.
However, I cannot extend java.awt.Window to add common classes of my two types of classes (let name them MyJWindow & MyJFrame respectively) as java.awt.Window's constructors are not public.
Hence, the solution is to use an association of MyJWindow & MyJFrame to another class (association instead of inheritance as most dp books say).
I thus have the following structure.
java.awt.Window
|
------ JWindow -|
| ----MyJWindow
|
|
------ JFrame -|
-----MyJFrame
and MyJWindow & MyJFrame associate with (lets say) MainWindow which contains the common methods of these two.
The MainWindowFactory uses an if statement and returns a type of Window object:
Window window = mainWindowFactory.selectGUI(which);
MainWindow can also play the role of a facade to other clients that want to use the GUI classes.
The problem is that in a method methodX() in MainWindow, MainWindow has to decide which specific method to call:
void methodX() {
if (window instanceof JFrame)
((MyJFrame) window).methodX();
else if (window instanceof JWindow)
(MyJWindow)window).methodX();
}
I was wondering if there is a way to avoid this as something similar has to be written for all the methods in MainWindow; trying the code:
window.methodX();
doesn't work of course as window is of type java.awt.Window and doesn't contain any methodX() method.
Any thoughts?
Regards,
John.
<<Less