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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:39:09+00:00 2026-06-02T23:39:09+00:00

I want to avoid using tagged classes and big if-else blocks or switch statement

  • 0

I want to avoid using tagged classes and big if-else blocks or switch statement and use polymorphism with a class hierarchy instead, which I believe is better practice.

For example, something like the below, where the choice of executed method is dependent only one one field of an object of type Actor.

switch(actor.getTagField())
{
    case 1: actor.act1(); break;
    case 2: actor.act2(); break;
    [...]
}

would become

actor.act();

and the act method would be overridden in subclasses of Actor.

However, the most obvious way to decide at runtime which subclass to instantiate looks awfully similar to the original:

Actor newActor(int type)
{
    switch(type)
    {
        case 1: return new Actor1();
        case 2: return new Actor2();
        [...]
    }
}

so it seems like nothing has really been gained; the logic has just been moved.

What is a better way to do this? The only way I can come up with involved implementing a factory class for each subclass of Actor, but this seems rather cumbersome for such a simple problem.

Am I overthinking this? It just seems like there’s no point making the original change if I just do pretty much the same thing elsewhere.

  • 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-02T23:39:10+00:00Added an answer on June 2, 2026 at 11:39 pm

    Question is “if” you need a factory. The factory is meant to manage the creation of instances an not so much the behavior of related instances.

    Otherwise, you’re just looking at basic inheritance. Something like..

    class Actor{
      public void act(){
        System.out.println("I act..");
      }
    }
    
    class StuntActor extends Actor {
      public void act(){
        System.out.println("I do fancy stunts..");
      }
    }
    
    class VoiceActor extends Actor {
      public void act(){
        System.out.println("I make funny noises..");
      }
    }
    

    To Use, you can just instantiate the type of actor you need directly.

    Actor fred = new Actor();
    Actor tom = new VoiceActor();
    Actor sally = new StuntActor();
    
    fred.act();
    tom.act();
    sally.act();
    

    Output:

    I act..
    I make funny noises..
    I do fancy stunts..
    

    EDIT:

    If you need to centralize the creation of the Actors..aka vis a Factory, you will not be able to get away from some kind of switching logic–in which case..i’ll typically use an enumeration for readability:

    public class Actor{
      public enum Type{ REGULAR, VOICE, STUNT }
    
      public static Actor Create(Actor.Type type){
        switch(type) {
          case VOICE:
            return new VoiceActor();
          case STUNT:
            return new StuntActor();
          case REGULAR:
          default:
            return new Actor();
        }
      }
    
      public void act(){
        System.out.println("I act..");
      }
    }
    

    Usage:

    Actor some_actor = Actor.Create(Actor.Type.VOICE);
    some_actor.act();
    

    Output:

    I make funny noises..
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Because I want to avoid repetitive code, and I'm using a lot of text
Let's say I want to avoid using bind variables in JDBC and run SQL
I am designing an Application using the Qt framework, and I want to avoid
I totally want to avoid using inline javascript! But i have a table which
I was reading this: Guideline Number 7: Try to avoid using unecessary classes and
I want to avoid using intents in android tabs. so I am doing views,
I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I want to avoid the duplication of stylesheet link tags in the output html
I want to avoid compiling php with fileinfo, ereg, and parch. What is the
I want to avoid code duplication as much as possible. Suppose I have a

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.