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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:30:55+00:00 2026-06-09T00:30:55+00:00

The idea I need to create Commands. Commands can be configured with parameters. Not

  • 0

The idea

I need to create Commands. Commands can be configured with parameters.
Not every command can receive the same parameters. So some have to be ignored.

I have an abstract class Command in which I’ve defined a Builder. By default every append parameter throws ‘UnsupportedOperationException’

public abstract class Command {

   public static abstract class CommandBuilder {

        // TODO instanceof. How to do this better?
        public CommandBuilder append(Parameter p)
            throws UnsupportedOperationException {

            if (p instanceof URLParameter)
                return append((URLParameter) p);

            if (p instanceof ActionParameter)
                return append((ActionParameter) p);

            if (p instanceof RepeatParameter)
                return append((RepeatParameter) p);

            if (p instanceof TimeOutParameter)
                return append((TimeOutParameter) p);

            return this;

        }

        public CommandBuilder append(URLParameter p)
                throws UnsupportedOperationException {

                    throw new UnsupportedOperationException(
                        "URLParameter not applicable");

        }

        public CommandBuilder append(RepeatParameter p)
            throws UnsupportedOperationException {

                throw new UnsupportedOperationException(
                    "RepeatParameter not applicable");

            }
            ...

}

If you want a parameter to be applicable onto a certain concrete Command, lets say an FTPCommand.

You will have to do something like this:

public class FTPCommand extends Command {

    public static class Builder extends CommandBuilder {

    @Override
    public CommandBuilder append(URLParameter p) {
            System.out.println("URLParemeter appended");
                return this;
            }
        }

}

So that when a URLParameter is provided it doesn’t throw an exception anymore but instead applies it.

But the client of this CommandBuilder might not be able to provide the concrete subclass. So in general a “Parameter” is given. But it needs to go to the right place (method)

Like a URLParameter must arrive at the append(UrlParameter p)

How can I do this in a clean(er) and nice(r) way? Because I’m not really ‘enthousiastic’ to use instanceof .

  • 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-09T00:30:57+00:00Added an answer on June 9, 2026 at 12:30 am

    This looks like a classic double-dispatch or visitor scenario. From the double-dispatch reference:

    a mechanism that dispatches a function call to different concrete
    functions depending on the runtime types of two objects involved in
    the call

    The Parameter and the CommandBuilder need to interact between themselves on what to do.

    The CommandBuilder can call back on the parameter. The Parameter objects all implement a common interface and the implementation of each subclass will differ.

    public CommandBuilder append(Parameter p) {
       // the append method called depends on the underlying type of 'p'
       p.append(this);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to archive data from a CRM system. Does anyone have any idea
Here is the idea I would like to develop. I need to have an
I have been reading that using HibernateDaoSupport isn't a good idea, so I need
I like the idea of Command Query Separation but can't see how to use
I need to debug remote java code with IntelliJ Idea 10.5 (Ultimate Edition) When
basically need to change the value that - admin_url() returns any idea?
I need to convert a String to enum. I followed the idea in String
I need to implement a SOAP Web Service on Jboss Seam 2.1.0. The idea
Has anyone an idea how pipebytes.com works ? I need to implement similar system
idea I would like to create a little app for myself to store ideas

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.