dcsimg
Can I avoid "if instanceof" statements in factory pattern?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: Can I avoid "if instanceof" statements in factory pattern?

Posted By:   Lance_Walton  
Posted On:   Wednesday, July 31, 2002 05:16 PM

Hi.

Based on your example, it seems like you want to treat MyJFrame and MyJWindow polymorphically (within your MainWindow.methodX()) in order to send the methodX() message to them.

So, create an interface that declares methodX() and all the other common methods that you need and make MyJFrame and my MyJWindow implement it.

Now you don't need the instanceof or the casts.

I think this will work :-)

Regards,

Lance

----

Lance Walton - lance@teaminabox.co.uk

Team In A Box - Software without Tragedy

http://www.teaminabox.co.uk

Re: Can I avoid "if instanceof" statements in factory pattern?

Posted By:   Stephen_McConnell  
Posted On:   Wednesday, July 31, 2002 05:43 AM

Have you thought of wrapping your JWindow and JFrame in another class in a Deligate design pattern? Then, your factory only creates one class which wraps the correct JFrame/JWindow...


Stephen McConnell

About | Sitemap | Contact