Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:30:41+00:00 2026-06-04T01:30:41+00:00

I’m a little bit unfriendly with Visitor pattern, but I have a task that

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:30:43+00:00Added an answer on June 4, 2026 at 1:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.