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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:30:20+00:00 2026-05-26T03:30:20+00:00

Is it better to use enum or String to dispatch to the right object

  • 0

Is it better to use enum or String to dispatch to the right object to create in a static factory method ?

Example with String :

public static Car createCar(String carName){
    if(carName.equals("Ferrari")){
        return new Ferrari();
    }
    else if(carName.equals("Porsche")){
        return new Porsche();
    }
    ....
}

Example with enum :

public static Car createCar(CarEnum carEnum){
    if(CarEnum.FERRARI.equals(carEnum)){
        return new Ferrari();
    }
    else if(CarEnum.PORSCHE.equals(carEnum)){
        return new Porsche();
    }
    ....
}

For now, according to me :

Benefit of using enum :

  • Avoid that the user calls the factory with a non-treated carName.

Drawback of using Enum :

Increase dependencies because a change into enum (FERRARI becoming ENZO_FERRARI for instance) will require modification on client. However, with String we could redirect Ferrari to Enzo Ferrari instance without recompile client code. Of course, we could make the same thing for client using old enum values with a redirection of FERRARI to ENZO-FERRARI enum but for me it implies that enum would have to keep old values to make client compatible… no make sense for me.

I would like to know what are your opinions on this subject?

  • 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-26T03:30:21+00:00Added an answer on May 26, 2026 at 3:30 am

    If, as you do in this case, you have a fixed number of acceptable arguments to a method, then it’s best to enforce that constraint as best as you can on the clients of the method. In this case, accepting any old string would cause more failures than using an enum.

    However, you might also consider, in a limited case like this, using named factory methods for each type of car: like createFerrari(), createPorsche() etc.

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

Sidebar

Related Questions

Why is it considered better practice to use enum not static const bool in
Is it better to use $.get(http://www.example.com/mydirectory, function(data) { $(.someclass).html(data); }); or $('.tripPlannerBottom').load(http://www.example.com/mydirectory); any speed
What is better to use for Exception output message dynamic generation String or StringBuilder
I have set of constants like: public static final String CONST1=abc; public static final
I have the following as an example: public enum HttpRequestHeader { Accept, AcceptCharset }
I've made the following extension method ... public static class ObjectExtensions { public static
Is better to use the below statement Convert.ToInt32(0 + stringValue) If not then why?
Which is better to use in PHP, a 2D array or a class? I've
Is it better to use a stored procedure or doing it the old way
Is it better to use NOT or to use <> when comparing values in

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.