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

  • Home
  • SEARCH
  • 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 8719567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:49:50+00:00 2026-06-13T06:49:50+00:00

I have a design question to ask. I so far have not written any

  • 0

I have a design question to ask. I so far have not written any code, just thinking about how to implement at this point. I have an object of type A and it contains an object of type B. I would like B to define a category of objects (Interface or abstract class?). I then have an xml file that defines the sub-type that B should be. For instance, if B is an interface named Driveable, then “car” or “truck” might be defined in the xml as the Drivable object needed for type A.

What I was thinking of doing is making B an interface and then creating a static factory class that has a factory method to determine what sub-type B should be given an xml file. So my first question is would this be the best way to approach the problem? Would it be better to use an abstract class instead of an interface or is it mainly just personal preference?

Second, if I do go with an interface, then inside my factory method would I just do:

B createB(File f){
    ...
    String type = ...
    if(type.equals("car"))
        return new CarType();
    else if(type.equals("truck"))
        return new TruckType();
    ...
    return null;
}

So every time I add a new B sub-type, I would need to add another if statement to this method. Is there a better way to do this so all I would have to do is create a new B sub-type and then update my xml and not update the factory method? I essentially dont want to hard code in the types of B into my factory method. If what I have above is pretty standard protocol with factory methods, then I can deal with doing it that way, just doesnt quite seem right.

  • 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-13T06:49:51+00:00Added an answer on June 13, 2026 at 6:49 am

    Growing off of carnold’s example, your could create a map in your Factory Class that maps between the text in the xml and the corresponding java class.

    private final static Map<String, Class> typeMap;
    
    static {
       typeMap = new HashMap<String, Class>();
       typeMap.put("car", CarType.class); 
       typeMap.put("truck", TruckType.class);
    }
    

    and then in your method:

    B createB(File f){
        ...
        String type = ...
        Class clazz = typemap.get(type);
        if (clazz == null){
            return null;
        }
    
        B newB = clazz.getConstructor(/*arg types*/).newInstance(/*args*/);
        return newB;
    }
    

    Slightly easier to maintain than the if/else, but still have to maintain it.

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

Sidebar

Related Questions

This is probably more a design or style question: I have just been considering
I have a question about best practices with the Module Design Pattern. The code
I was going to ask a question here about whether or not my design
So I have been trouble thinking of a way to ask this question so
This is more of a design question. I have a template class, and I
This is more of a general design question I guess... I have an Ajax
I have posted a question about multilanguage database design here, [] What are best
I've a general design question: I have a mailserver, written in C#. Then I
I have tried to ask a variant of this question before. I got some
I want to ask a question that, Is there any method to design 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.