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 9018557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:33:29+00:00 2026-06-16T04:33:29+00:00

I have the following base interface: public interface Value{ double getValue(); } I would

  • 0

I have the following base interface:

public interface Value{

double getValue();
}

I would like to have a few different Outputter interfaces that would take a Collection and output them, based on the CONCRETE type of the Value, so I could have a Value class that would have 10 other important fields, which should also be output. I know this falls nicely into a Visitor pattern, but my concern is this: Each outputter would always get a list of ONE specific type of Value, SO I could, in theory, supply a typed list of MeanValue, StdDevValue etc. I am not sure how to design this nicely, as Values are generated by another interface, so in fact, I am only holding a reference to Collection, and I do not want to downcast it and then call specific method on the Outputter…

  • 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-16T04:33:30+00:00Added an answer on June 16, 2026 at 4:33 am

    The Visitor pattern doesn’t involve any casting. That’s the point of it.

    Here’s the core of a Visitorised family of values:

    interface Value {
        double getValue();
        void accept(ValueVisitor visitor);
    }
    
    interface ValueVisitor {
        public void visit(MeanValue value);
        public void visit(StdDevValue value);
    }
    
    class MeanValue implements Value {
        @Override
        public double getValue() {
            // whatever
        }
    
        @Override
        public void accept(ValueVisitor visitor) {
            visitor.visit(this);
        }
    }
    
    class StdDevValue implements Value {
        @Override
        public double getValue() {
            // whatever
        }
    
        public int getDegreesOfFreedom() {
            // here's a subclass-specific method
        }
    
        @Override
        public void accept(ValueVisitor visitor) {
            visitor.visit(this);
        }
    }
    

    Here’s the output bit:

    abstract class Outputter implements ValueVisitor {
        public void output(Collection<? extends Value> values) {
            for (Value value : values) {
                value.accept(this);
            }
        }
    }
    
    class PrintingOutputter extends Outputter {
        @Override
        public void visit(MeanValue value) {
            System.out.println("Mean: " + value.getValue());
        }
    
        @Override
        public void visit(StdDevValue value) {
            System.out.println("Std Dev: " + value.getValue() + " (" + value.getDegreesOfFreedom() + ")");
        }
    }
    

    You can use an Outputter with either a collection of mixed values, or a collection of a specific kind:

        List<Value> mixedValues = /* whatever */;
        outputter.accept(mixedValues);
    
        List<MeanValue> meanValues = /* whatever */ ;
        outputter.accept(meanValues);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following interfaces defined in a base library project: public interface IWorkContext
I have a service-layer Interface that extends from a base Interface; I would like
I have the following base interface public interface IHandler{ void Handle(IMessage message); } and
I have the following base interface public interface IBaseAction { bool CanAct(...) } and
Suppose I have the following (trivially simple) base class: public class Simple { public
I have the following ViewModel, which is the base class of other ViewModels that
I have the following situation: // A public interface of some kind public interface
Let's say I have the following class hierarchy (base interface included): IAction -> (abstract)
I have defined the following interface: public interface IHaveAProblem { string Issue { get;
I have the following generic question Here is a my generic message interface. public

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.