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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:36:30+00:00 2026-06-14T11:36:30+00:00

I have created factory class and i wonder which is better way to implement

  • 0

I have created factory class and i wonder which is better way to implement it.
option 1

public class Factory {

    private IProperty prop;
    public IDoc doc;

    public Factory(int version) {
        switch (version) {
            case '1':   
                prop = new Prop();
                doc = new Docu();
        ...
            case '2':
            prop = new Prop1();
            doc = new Docu1();
        ...
        }
    }

    public IProperty getProperty() {
        return this.prop;
    }

    public IDoc getDoc() {
        return this.doc;
    }
}

My question is if to do it like that i.e. define member with the interface type and to to switch on the constructor or for every get method to use switch statement instead on the constructor, so in the constructor i will just get the
version and save it on class member and than for instance use like

public IProperty getProperty() {
switch (version) {
  case '1':
    prop = new Prop();
  case '2':
    prop = new Prop1();
...

So what is the better way, or any other idea?

  • 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-14T11:36:32+00:00Added an answer on June 14, 2026 at 11:36 am
    • First of all, your Factory should ideally have static references
      instead of non-static one. And it should have a static method to create / get
      the appropriate instance.

    • Secondly, its better to have two different factories for different
      types.

    • Thirdly, I would name your method as createProperty, rather than
      getPropertyObject, because that method is not returning the already
      created instance, rather its creating one.

    Of course, getPropertyObject('1'), seems like it is fetching a property for that version from a persistence storage, which is not what it is doing. It is rather creating an instance based on version.

    (NOTE: – Name of static factory methods are important. They are amongst one of the advantages, they have over constructors. Since with a name, you can guess what exactly that factory method does)

    Having said that, I would say, that the 2nd option would be better with all those changes. Let the createProperty method decide, how it wants to create the instance.

    So, I would modify your code like this: –

    public class PropertyFactory {
        private static IProperty prop;
    
        public static createProperty(char version) {
    
            switch (version) {
                case '1':
                    prop = new Prop();
                    break;  // Don't forget a `break` here.
                case '2':
                   prop = new Prop1();
                   break;
    
                default:  // do have a default case
                   prop = null;
            } 
            return prop;
        }
    }
    

    Similarly, you can create a DocumentFactory to create a document object. Name the method: – createDocument(char version)

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

Sidebar

Related Questions

I have created a factory class called AlarmFactory as such... 1 class AlarmFactory 2
I have a user object called UserSystem, which is created by a static factory
I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances
I have a stateless session bean which needs access to a factory class. Is
I have created a File class, which takes care of all operations on files,
I have an Object Factory Class which has a method called registerCreator as defined
i have created class cl_DBHandler which extends SQLiteOpenHelper . and i have created class
Inside a container object, I have created a class Factory , that is responsible
I have created two imageViews promatically as shown below: public void createImageViews(Integer count){ ImageView[]
I have a utility class library I am writing. A have written a factory

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.