I’m a little bit unfriendly with Visitor pattern, but I have a task that needs to have Visitor implementation (if I want to avoid “instanceof” checks).
I have a class that is a wrapper for several gwt elements: Label, Panel, Widget (can be checkbox, listbox, textbox etc.). I use an array as a collection of alike parts of UI. E.g. Label + checkbox, Label + text box; Label + button etc.
Some of elements are constructed in a different way (part of another class derived from, for example, Panel). So as a result I have two constructors that are the same, but in one place overloaded method is used. I can merge these constructors and check the element using “instanceof” inside mentioned method. But I dislike this solution and want to substitute it using Visitor pattern. To tell the truth, I don’t know how to do it and hope for you help.
Here is an example of what I have:
public class MyWidgets {
private String stringLabel;
private Widget widget;
private Panel panel;
public MyWidgets(String stringLabel, Widget widget) {
this.stringLabel = stringLabel;
this.widget = widget;
initPanel(stringLabel, widget);
}
public MyWidgets(ConstructedClass cs, Widget widget) {
this.widget = widget;
initPanel(cs, widget);
}
private initPanel(String label, Widget widget) {
panel = SomeStaticUtilityClass.initPanel(new Label(label), widget);
}
private initPanel(ConstructedClass cs, Widget widget) {
panel = SomeStaticUtilityClass(cs, widget);
}
}
Something like this (I tried to make it maximum abstract, in reality it is more difficult).
So I have a solution using “instanceof”:
private initPanel(Object object, Widget widget) {
if(object instanceof String) {
panel = SomeStaticUtilityClass.initPanel(new Label(label), widget);
}
if(object instanceof ConstructedClass) {
panel = SomeStaticUtilityClass.initPanelFromObject(cs, widget);
}
}
I want to be saved from “instanceof” and leave just one constructor and even, if it is possible, one init method without its overloaded version.
Thank you in advance for you suggestions, help.
P.S> I repeat, that the class above is fabricated, and looks like some misunderstanding especially with this String label 🙂
IMO, your existing solution, with two constructors, is fine.
You could use the strategy pattern and have your constructor take an instance of some
PanelProviderinterface instead of Object. This interface would have the following method:. Clients would pass an instance of
StringPanelProvideror an instance ofConstructedClassPanelProviderto the constructor. Your constructor would thus look like:And the StringPanelProvider implementation would look like
The ConstructedClassPanelProvider would look the same.
If you really want to use the Visitor pattern then you’ll have to modify the above a little bit:
But this looks like overengineering to me.