How to use Visitor pattern for substituting "instanceof"

gwt, instance, java, polymorphism

Solution

IMO, your existing solution, with two constructors, is fine.

You could use the strategy pattern and have your constructor take an instance of some `PanelProvider` interface instead of Object. This interface would have the following method:

Panel createPanel(Widget widget);

. Clients would pass an instance of `StringPanelProvider` or an instance of `ConstructedClassPanelProvider` to the constructor. Your constructor would thus look like:

public MyWidgets(PanelProvider panelProvider, Widget widget) {
   this.widget = widget;
   this.panel = panelProvider.createPanel(widget);
}

And the StringPanelProvider implementation would look like

public class StringPanelProvider implements PanelProvider {

    private String s;

    public StringPanelProvider(String s) {
        this.s = s;
    }

    @Override
    public Panel createPanel(Widget widget) {
        return SomeStaticUtilityClass.initPanel(new Label(s), widget);
    }
}

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:

public interface Visitable {
    void accept(Visitor visitor);
}

public interface Visitor {
    void stringVisited(String s);
    void constructedClassVisited(ConstructedClass cs);
}

public class StringVisitable {
    private String s;

    public StringVisitable(String s) {
        this.s = s;
    }

    void accept(Visitor visitor) {
        visitor.stringVisited(s);
    }
}

// similar for ConstructedClassVisitable

public MyWidgets(Visitable visitable, final Widget widget) {
   this.widget = widget;
   visitable.accept(new Visitor() {
       public void stringVisited(String s) {
           panel = SomeStaticUtilityClass.initPanel(new Label(label), widget);
       }

       public void constructedClassVisited(ConstructedClass cs) {
           panel = SomeStaticUtilityClass.initPanelFromObject(cs, widget);
       }
   });
}

But this looks like overengineering to me.

Problem

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 :)

Original source