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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:41:47+00:00 2026-06-06T23:41:47+00:00

I need a design pattern to pass arguments to a subclass, however the arguments

  • 0

I need a design pattern to pass arguments to a subclass, however the arguments differ.

1) Unlimited instanceof checks: Problems: a) slow b) not readable.

Component a ;
if ( a instanceof SimpleComponent)
{
   (SimpleComponent)a.do();
}
else if ( a instanceof UnitComponent)
{
   (UnitComponent)a.do( unit );
}
else if ( a instanceof BuildingComponent)
{
   (BuildingComponent)a.do( building );
}

1a) Use Setter: Same as above instead do(X), call set(X), requires unlimited instanceof checks.

2) Huge switch statement: Problems: a) we still have to cast instances. b) not readable.

3) pass the variables on constructor: Problems: Cannot change that variable again in the lifetime of application, that would require a call to a specific unknown .set(X) Method.

4) Put all unreleted variables to superclass:

   Component
   {
     Unit unit;
     Building building;
     String i_can_pass_strings_0;
     String i_can_pass_strings_1;
     String i_can_pass_strings_2;
     int    i_am_allowed_to_pass_a_single_int_now;
   }

This is like using global variables for the shake of passing variables between functions.
You will end with a class with 200-300 unrelated variables, problems with garbage collection. Also you must be trolling for even thinking it, the class ends as a big unreadable garbage bin.

5) The above Component class renamed to ComponentOptions (it still is a garbage bin).

interface Component
{
   do ( ComponentOptions );
}

solution: at least now i don’t have to think how i call do(); it is called the same way.

problems: a) instead of the caller deciding what variables to pass, the receiver subclass has to decide how to read the variables. In this case we use ComponentOptions which is our garbase disposal bin.

6) Component pattern: has/get Component.

class UnitComponent implement Component
{
   public void do( ComponentOptions componentOptions )
   {
      if ( hasComponent( Unit.class )
      {
         Unit unit = componentOptions.getComponent( Unit.class );
         doStuff( unit ) ;
      }
   }
}

a) readable

Question:
1) is there another design pattern to solve this problem, or is solution 6 the best way to solve this?

  • 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-06T23:41:49+00:00Added an answer on June 6, 2026 at 11:41 pm

    One of known substitutes of spamming instanceofs is Visitor pattern – take a look here: http://www.refactoring.com/catalog/replaceConditionalWithVisitor.html

    you can also read this question Avoiding instanceof in Java

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

Sidebar

Related Questions

Isn't there a Design Pattern who describes how to high cohesion? I need some
why do we need a Abstract Factory design pattern to create objects? Why can’t
I need to write a program implementing the visitor design pattern. The problem is
Ok - I've been looking at the MVC design pattern, and need to clarify
Sometimes when I need lazily initialized field, I use following design pattern. class DictionaryHolder
Why do we need design patterns like Factory, Abstract Factory and Singleton?
So, I need to write an example code using the decorator dessign pattern and
We need to design a secure web application. I would like to propose a
I need to design a translation mechanism/strategy for the static text in my (scalable)
I need to design a JSON interface which when implemented on my end will

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.