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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:44:52+00:00 2026-05-31T13:44:52+00:00

I have a question about code style: Let’s assume i have a String containing

  • 0

I have a question about code style:

Let’s assume i have a String containing some information (like “Information1” or “Information2”). Based on this i want to create objects with a factory. Obviously i could write something like this:

if(string.equals("Information1")){
  Factory.createInformation1Object();
}
if(string.equals("Information2")){
  Factory.createInformation2Object();
}
if(string.equals("Information3")){
  Factory.createInformation3Object();
}

Now i was wondering if there is a better (and prettier) way to do this. I really like the multiple dispatch idea of the visitor pattern, but i cant see a way to apply this easily to this particular problem.

  • 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-31T13:44:53+00:00Added an answer on May 31, 2026 at 1:44 pm

    You can use the abstract factory pattern.

    Create an abstract Factory class [or interface], and classes that extends it: MyObject1Factory, MyObject2Factory, …

    On preprocessing, populate a Map<String,Factory> from a String to the coresponding Factory instance, this is done only once in your application.

    When you need to create a new instance – invoke map.get(string).create() to create the relevant object, of the relevant type.

    Edit: Small example with code:

    Your classes are:

    public static class MyBase { 
        @Override
        public String toString() {
            return "Base";
        }
    }
    public static class Class1 extends MyBase { 
        @Override
        public String toString() {
            return "Class1";
        }
    }
    public static class Class2 extends MyBase { 
        @Override
        public String toString() {
            return "Class2";
        }
    }
    

    And your factories will be:

    public static abstract class MyFactory {
        public abstract MyBase build();
    }
    public static class MyFactory1 extends MyFactory {
        @Override
        public Class1 build() {
            return new Class1();
        }
    }
    public static class MyFactory2 extends MyFactory {
        @Override
        public Class2 build() {
            return new Class2();
        }
    }
    

    populate a map only once in the program lifetime:

        Map<String,MyFactory> map = new HashMap<String, Test.MyFactory>();
        map.put("class1", new MyFactory1());
        map.put("class2", new MyFactory2());
    

    and when you need a new object, invoke with:

        MyBase obj = map.get(s).build();
        System.out.println(obj);
    

    (*) Note: The static keyword for classes are here because I created them as inner classes – of course you need to ommit it if it is not your case….

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

Sidebar

Related Questions

i have what i hope is a quick question about some code i am
I have a question about the following C code: void my_function() { int i1;
I have a question about setting the style attributes for a data cell in
I have a question about usability/design. I currently am using some JQuery to hide/show
In a coding style question about infinite loops , some people mentioned they prefer
I have a question about how to read the string betweena tag, for example.
I have a question about Java style. I've been programming Java for years, but
I have the following code but my question is about the banner image <!DOCTYPE
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and

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.