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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:03:53+00:00 2026-05-31T14:03:53+00:00

I hope this isn’t too specific. I have created an XML Schema which i

  • 0

I hope this isn’t too specific.

I have created an XML Schema which i compile with XJC to get the classes.
The XML basically represents some elements of a form (like textfield, labels etc).
Here’s an excerpt:

<gruppoOggetti id="string" nome="string">
        <oggetto xsi:type="labelType" etichetta="string" id="string" obbligatorio="false" />
        <oggetto xsi:type="listaOpzioni" id="string" obbligatorio="1">
            <opzione id="string">string</opzione>
        </oggetto>
        <oggetto xsi:type="imageType" etichetta="string" id="string" obbligatorio="0" />
    </gruppoOggetti>

As you can see, the type of the object is defined from the attribute xsi:type so when i unmashall the xml with jaxb it automatically instatiate the right classes.

Now the question:
I’m implementing an abstract factory to create the objects so basically i have to call a different method of my factory depending on which object the xml says i have to create.
The not-quite-satisfying method i’m using right now is this:

public OggettoBase creaOggetto(Factory f, OggettoType oggetto)
{
    String tipo = oggetto.getClass().getSimpleName().toString();
    OggettoBase ret = null;
    switch(tipo)
    {
        case "CheckBoxType": ret = f.createCheckbox(); break;
        case "ImageType":ret = f.createImage(); break;
        case "LabelType":ret = f.createLabel(); break;
        case "LinkType": ret = f.createLink(); break;
        case "ListaOpzioni": ret = f.createLista(); break;
        case "PasswordType": ret = f.createPassword(); break;
        case "RadiobuttonType": ret = f.createRadiobutton(); break;
        case "TextareaType": ret = f.createTextarea(); break;
        case "TextfieldType": ret = f.createTextfield(); break;
        default : System.out.println("Il tipo: "+tipo+" non esiste");
    }
    return ret;
}

I want to use an other method (edit: instead of the switch/case), and i was thinking about enums but i’m not quite there. Also i don’t want to use reflection.

  • 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-31T14:03:54+00:00Added an answer on May 31, 2026 at 2:03 pm

    Well, to me this is not really a factory pattern. A factory pattern usually is coupled with the Strategy Pattern. So my suggestion would be to implement your factory so that it just calls f.CreateObject which will return an object that can call a more generic Create(). The nice thing about this is that it allows your methods and classes to focus on their particular responsibility, and anytime you have to change functionality for one item, then you dont have to rebuild others (you can work off of a base class if you have common functionality that must change for all, too). Let me know if you need further details on below or the reasoning. You can probably even make the main method more of your factory and pretty the code even more.

    public OggettoBase creaOggetto(OggettoType oggetto)
    {
        String tipo = oggetto.getClass().getSimpleName().toString();
        IObjectCreator creator = Factory.GetObjectCreator(tipo);
        if(creator == null)
            return null;
        return creator.Create();
    }
    
    public interface IObjectCreator
    {
        OggettoBase Create(); 
    }
    
    public CheckboxCreator:IObjectCreator
    {
        public OggettoBase Create()
        {
            return new Checkbox();
        }
    }
    
    //Repeat for each type
    
    public static Factory
    {
        public IObjectCreator GetObjectCreator(string tipo)
        {
            IObjectCreator creator;
            switch(tipo)
            {
                case "CheckBoxType": creator = new CheckboxCreator(); break;
                ...
                default : System.out.println("Il tipo: "+tipo+" non esiste");
            }
            return creator;
    }
    

    PS. I come from a C# background, so if some of the syntax is off, please let me know. But, the basic principle still applies

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

Sidebar

Related Questions

OK I hope this isn't too specific. I have a database driven CMS that
I'm trying to get this to work and I hope this isn't too vague.
I hope this isn't too simple, but I have a report retrieves rows using
I hope this isn't too off topic for this forum, but I have been
I hope this isn't too difficult, obviously you can call HTML tags such as
Alright, I hope this isn't too broad a question but my curiosity got the
Hey guys, hope this isn't too much of a n00b question I'm mainly a
I hope this question isn't too open ended, but a nudge in the right
I'm relatively new to web application programming so I hope this question isn't too
I've seen a few questions dancing around this, so I hope this isn't too

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.