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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:05:21+00:00 2026-06-09T01:05:21+00:00

I have the below objects interface Adapter<T> { T fromXml(XML xml); XML toXml(T obj);

  • 0

I have the below objects

interface Adapter<T> {
      T fromXml(XML xml);
      XML toXml(T obj); //ignoring this in implementations
}

abstract class AbstractAdapter<T> implements Adapter<T>{
       T fromXml(XML xml) { //template method
         validate(xml);
         return parse(xml);
       }
       abstract void validate(Xml xml);
       abstract T parse(Xml xml);
}

class MyObjectAdapter extends AbstractAdapter<MyObject> {
       MyObject parse(XMl xml) {return null;}
       void validate(Xml xml) {}
}

class AnotherObjectAdapter extends AbstractAdapter<AnotherObject> {
       AnotherObject parse(XMl xml) {return null;}
       void validate(Xml xml) {}
}

class AdapterFactory {
      Adapter<?> createAdapter(String objectType) {
         if (objectType == "MyObject") return new MyObjectAdapter();
         if (objectType == "AnotherObject") return new AnotherObjectAdapter();
}

public static void main(String args[]) {
      AdapterFactory factory = new AdapterFactory();
      //********
      Adapter<MyObject> myobjectAdapter = (Adapater<MyObject>) factory.createAdapter("MyObject");
      MyObject obj = myobjectAdapter.fromXml(getXml(args)); 
}

How do I avoid the downcasting I do in the line marked ** above?
The objects that need to have an adapter do not have any relation between them, they are generic data objects.

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

    You have an class name in a String and you’re trying to go from that to the actual type. That is always going to involve a cast. If you remove the AdapterFactory and hence the String, then there is no problem.

    (Usual note that == on Strings isn’t typically what you want.)

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

Sidebar

Related Questions

I have a listView with custom objects defined by the xml-layout below. I want
I have to generate XML in the below format <objects> <items> <item =delete> <searchfields>
I have an abstract class, AbsClass that implements an interface, IClass . IClass has
I have this class: @interface G2Matrix : NSObject ... - (id) initWithArray:(float *)val; ...
I have a figure below: And my question is that the objects in Array
I have a large (more than 100K objects) collection of Java objects like below.
I have a NSMutableArray of objects, below is the struct: //object at index i
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
I have this object below: I thought about putting the messages in html like
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)

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.