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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:29:00+00:00 2026-06-05T01:29:00+00:00

I need to load objects from a file. I don’t know how to design

  • 0

I need to load objects from a file. I don’t know how to design the interface of classes that support this operation.

public interface TestComponent {
    TestComponent loadFromXML(Element _xmlElement); 
}

I can’t make the method loadFromXML static, and I can’t declare (not define!) a constructor –
but it’s pretty ugly to create a Class with the single purpose to call loadFromXML. It is also ugly to separate those mechanics within a utility class that calls a specified constructor within a switch-case.

How do you design an interface in that case? Should I maybe use abstract classes?

  • 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-05T01:29:02+00:00Added an answer on June 5, 2026 at 1:29 am

    I agree that the version in your question looks a bit unnatural.

    Here are some alternatives:

    1. You could add a void load(...) to the TestComponent interface that will load “this” object with state from the XML. (This is analogous to the load and loadXML methods on java.util.Properties.)
    2. You could define a constructor in each TestComponent implementation classes that parses the XML to get the initial state of the object.
    3. You could create a static helper method.
    4. You could create a TestComponentFactory interface that has a TestComponent load(...) method that returns a loaded object.

    The different approached have advantages and disadvantages. For instance, the factory object approach (4.) separates the parsing / loading logic from the TestComponent class, but requires an extra class and interface. On the other hand the other three approaches tend to be cumbersome if you have multiple implementations of the main interface. The main problem is that you con’t do polymorphism with constructors in Java.

    Finally, if I was designing this, I’d make the load(...) method take a stream or reader as argument, and do the XML parsing itself (by calling the relevant library code). That way you can take care of all of that in one place (per TestComponent implementation class).

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

Sidebar

Related Questions

I need to load a .xml file from a URL adress into an NSData
I need to load a file from an umounted TrueCrypt disk into memory. Is
I need to load font ( .otf or .ttf ) file from java Resource
I need to select an item from a list box that contains objects of
When my application starts I need to load about 500 objects from an xml
I am saving some custom created Objects classes (Stream data) to File. I need
i need to load a library from my module using Modular Extensions my structure
I need to load an image from a web in a simple Java stand
I need to load a model, existing of +/- 20 tables from the database
I need to load some resource from my DLL (i need to load them

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.