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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:33:32+00:00 2026-06-12T01:33:32+00:00

For converting a Java object model into XML I am using the following design:

  • 0

For converting a Java object model into XML I am using the following design:

For different types of objects (e.g. primitive types, collections, null, etc.) I define each its own converter, which acts appropriate with respect to the given type. This way it can easily extended without adding code to a huge if-else-then construct.

The converters are chosen by a method which tests whether the object is convertable at all and by using a priority ordering. The priority ordering is important so let’s say a List is not converted by the POJO converter, even though it is convertable as such it would be more appropriate to use the collection converter.

What design pattern is that?

I can only think of a similarity to the command pattern.

  • 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-12T01:33:34+00:00Added an answer on June 12, 2026 at 1:33 am

    Well, you can start by trying to categorize the thing you want to do (output an XML file, convert something to something). The design patterns fall into three categories;

    • Structural pattern
    • Creational pattern
    • Behavioral pattern

    In this case you have two types of classes, an xml writer and some converters. The xml writer is basically a builder (it creates a file)

    XmlWriter writer = new XmlWriter();
    writer.writeHeader();
    for (Item item : xmlitems) {
        writer.write(convert(item));
    }
    writer.close();
    

    Now, the actual conversion of a class to xml is done by a few classes. You mention you have a method which tests the classes and directs them to a specific converter. This class can be argued to create a new instance of something, so it falls into the creational patterns.

    There are three types of patterns that would be suitable IMO.

    • Abstract factory pattern, which provides an interface for creating related or dependent objects without specifying the objects’ concrete classes.

    • Builder pattern, which separates the construction of a complex object from its representation so that the same construction process can create different representation.

    • Factory method pattern, which allows a class to defer instantiation to subclasses.

    Source: Wikipedia

    Either one is appropriate according to me. The builder pattern is appropriate since the implementation is sort of like

    public interface Converter {
        void convert(Item item);
        XmlTextNode getResult(); // get xml code
    }
    

    i.e. you give the class something and you get a result.

    The factory pattern is appropriate since you defer instantiation to some other class (your redirect method you spoke of)

    public XmlTextNode convert(Item item) {
        if (item instanceof ConcreteItem) {
            return new ConcreteConverter(item).getResult();
        }
        throw new InvalidOperationException("Invalid convert type");
    }
    

    In either case the actual type of the returned item is not important. It depends a little on where you want to "define the pattern". Is it in the method where you switch types, or in the actual creation/converter class.

    Then again, I’m no expert in the case.

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

Sidebar

Related Questions

I'm having trouble converting objects into JSONObject (org.json.JSONObject) in java. I have an object
I'm converting a Javascript program I wrote into Java, and there's one Object which
I have the following java code: nameFinderModel = NameFinderME.train(en, organization, sampleStream, Collections.<String, Object>emptyMap()); And
What is the most concise way of converting a java.util.List into a normal JavaFX
I'm looking for a Java library for converting values that supports generic types. That
I'm converting some Java code to Javascript, and the Java object has a Static
Newbie to C++ learning by converting a java program to c++. The following code
I'm currently working on a project where I'm converting database info to java objects,
I'm having trouble converting a java SortedMap into a scala TreeMap. The SortedMap comes
Spring MVC + Jackson converting Java object to/from Json data. I want to validate

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.