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

  • Home
  • SEARCH
  • 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 6733293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:45:05+00:00 2026-05-26T10:45:05+00:00

Here is some of my Java code public List<OBJ> a = new ArrayList<OBJ>(); public

  • 0

Here is some of my Java code

public List<OBJ> a = new ArrayList<OBJ>();
public String A;
public String B;
public String C;
for (OBJ o : a) {
    // .... TODO
}

So I have an interface OBJ and there are three objects that implements OBJ say X, Y, Z. So I store X/Y/Z objects in List a. Now say that I want to go through the loop and if o is of instance X store X.value in A, if Y store Y.value in B, and if Z store Z.value in C. So the problem is really how do you figure out what object type o is (X,Y,Z) to store their values in the right string.

NOTE: I want to use the Visitor pattern or something like it, but I don’t really have a firm grasp of it so I’m asking for your help.

This means NO Instanceof(s) or Type Casts and NO Dedicated Methods like

interface OBJ {
    void blah();
}

class X implements OBJ {
    public void blah();
} // etc

Thanks! I really want to get this import aspect of software engineering down!

Hey wow thanks for the detailed and fast responses, but my situation is a bit more complicated and sorry I didn’t add this before.

So String A, B, C are actually housed in another class like

class ARGH {
    public List<OBJ> a = new ArrayList<OBJ>();
    public String A;
    public String B;
    public String C;
    //invisible constructor here
    public String toString () {
        for (OBJ o : a) {
            // .... TODO
        }
        return "some string"
    }
}
public void main (String[] args) {
    ARGH argh = new ARGH();
    // Setup some X, Y, Z objects and their values here
    String D = argh.toString();
    // Do something to D
}

So the Strings and List are actually not global variables so I don’t think this would work:

ObjVisitor v = new ObjVisitor() {
    @Override
    public void visit(X x) {
      A = x.value();
    }
    // And so on.
}

I am assuming I have to somehow pass in the String A, B, C into the visit method but I don’t know how to do that and still stay with The Visitor Pattern.

  • 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-05-26T10:45:06+00:00Added an answer on May 26, 2026 at 10:45 am

    In a nut-shell you’d do like this:

    1. Create a Visitor interface ObjVisitor with one visit-method for each type.
    2. Add an abstract accept(ObjVisitor v) to OBJ.
    3. Add an accept(ObjVisitor v) { v.visit(this); } to each OBJ implementation.
    4. Call o.accept(yourVisitorImpl) in the loop.

    You did indeed get some code from Bringer128. I elaborated a bit and added the String-stuff.

    import java.util.*;
    
    interface OBJ {
        String accept(ObjVisitor v);
    }
    
    interface ObjVisitor {
        String visit(X x);
        String visit(Y y);
        String visit(Z z);
    }
    
    class X implements OBJ {
        public String accept(ObjVisitor v){ return v.visit(this); }
    }
    class Y implements OBJ {
        public String accept(ObjVisitor v) { return v.visit(this); }
    }
    class Z implements OBJ {
        public String accept(ObjVisitor v) { return v.visit(this); }
    }
    

    Usage:

    class Test {
        public static void main(String[] args) {
    
            List<OBJ> objs = Arrays.asList(new Z(), new X());
    
            ObjVisitor toStringVisitor = new ObjVisitor() {
                public String visit(X x) { return "X object"; }
                public String visit(Y y) { return "Y object"; }
                public String visit(Z z) { return "Z object"; }
            };
    
            String result = "";
            for (OBJ o : objs)
                result += o.accept(toStringVisitor) + "\n";
    
            System.out.println(result);
    
            // Prints
            // Z object
            // X object
        }
    }
    

    An alternative (perhaps better approach) would be to let the visitor implementation maintain a StringBuilder, let the visit-methods return void, and after the loop just call stringVisitor.getCurrentString() or something like that.

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

Sidebar

Related Questions

I'm looking at some code with this form: 1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList;
I am relatively new to Java and while trying out some code came across
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
Here is some code I could not get to format properly in markdown, this
Here's some Ruby code: puts %x{ pstree #{$$} } # never forks puts %x{
Here's some code I saw once. Can you see what's wrong with it? [updated]
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
I'm very new to Java and am having some issues looping through JCheckBoxes on
I have troubles using the java ServiceLoader in a NetBeans module application. Here is
Good evening everyone! I am working on learning some java and I have made

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.