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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:08:14+00:00 2026-06-10T23:08:14+00:00

I am having a class named DBObject which returns two different class objects.If the

  • 0

I am having a class named DBObject which returns two different class objects.If the input string is water it returns object1 else object2.

My requirement is , now it it is returning objects based on if else…but in future some more objects will be added. So, instead of using elseif which makes code static, is there any dymanic way to return the class objects?

I mean can i use any java design pattern in this scenario?

code:

if(str=="water")
 return new object1()
else if...
     return new object3()
else if...
     return new object4()
else if...
     return new object5()
else if...
     return new object6()  
else
  return new object2()
  • 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-10T23:08:16+00:00Added an answer on June 10, 2026 at 11:08 pm

    You’re talking about implementation level here, not design level. For implementation you need Java Reflection, which you can use like:

    Object obj = Class.forName(type).newInstance();
    

    On design level Abstract Factory is the closest pattern.

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

Sidebar

Related Questions

I having a class named Shape which has two subclasses Shape1 and Shape2. In
I'm currently having a Class named SqlData, which contains all the Methods needed to
I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I am having a inner class(static) named SelectProdLineAssociationForm I have also given a declaration
I am having a Server Class which has delegate methods of NSURLConnection. And I
I'm having really weird situation. I create a singletone object of a class named
I have a demo project-named A having a class Class AA. and I have
Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name=
Having such class can I instatiate it in place? abstract class Mammal { public
I am having a class Student. I want to store the object of class

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.