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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:58:12+00:00 2026-05-29T07:58:12+00:00

I want to create a Factory which returns dao-instance, depending on the Class clazz

  • 0

I want to create a Factory which returns dao-instance, depending on the Class clazz

Teammember, Scene and Equipment are my Model Classes.

My DAO’s look like this:

public class JDBCTeammemberDAO implements JdbcDAO<Teammember>

my Factory looks like this:

public class DAOFactory {

    JdbcDAO createDAO(Class clazz) {
        if(clazz.equals(Teammember.class)) {
            return new JDBCTeammemberDAO();
        }
        if(clazz.equals(Scene.class)) {
            return new JDBCSceneDAO();
        }
        if(clazz.equals(Equipment.class)) {
            return new JDBCEquipmentDAO();
        }
        return null;

    }
}

I was thinking about switch and polymorphism, but I couldn’t figure out how.

Basically I want to find the Implementation “SomeClass implements JdbcDAO”

My first approach was:

String name = clazz.getName().substring(6); // model.Teammember
Class<?> forName;
try {
    forName = Class.forName("dao.jdbc.JDBC" + name + "DAO");
    return (JdbcDAO) forName.newInstance();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

but I don’t feel good with handling this with String method. Besides, it doesn’t work, if I have different Model and Dao names (like: JDBCMemberDAO instead of JDBCTeammemberDAO)

  • 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-29T07:58:12+00:00Added an answer on May 29, 2026 at 7:58 am

    I was in a similar situation and decided to use a Dao registry to handle the issue. Using the generic dao pattern @Perception mentioned:

    public interface JdbcDAO<T> {
        T find(Long id) ;
        T create(T entity);
        T update(T entity);
        void delete(T entity);
    }
    
    public class JdbcDAOImpl<T> {
        this.clazz = clazz;
        DaoRegistry.register (clazz, this);
    }
    

    Then you can have your

       public class JDBCTeammemberDAO extend JdbcDAOImpl<TeamMember> {
           public class JDBCTeammemberDAO () {
              super(TeamMember.class);
           }
       }
    

    DaoRegistry would look something like this:

    public class DaoRegistry {
            private Map<Class, JdbcDAO> daoMap;
    
            public synchronized void register (Class type, JdbcDao dao) {
                    if (!daoMap.containsKey(type))
                         daoMap.put (type, dao);
                    else
                         logger.error ("Something is really wrong because you are creating another dao for this class.", e);
    
                }
    
            public JdbcDAO get(Class type) {return daoMap.get(type);
    }
    

    This is just the jest of it, you will need to make sure it is thread-safe. Hope this helps.

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

Sidebar

Related Questions

I want to create a factory which will create a smack XMPPConnection. The factory
I want to use this for an object factory: Given a string, create a
I want to create an allocator which provides memory with the following attributes: cannot
Code below is pseudo code. Imagine a class Fruit which has a factory method
I want to mock/stub: @the_bill = GovKit::OpenCongress::Bill.find_by_idents(112-s368).first for use in my tests. which returns
Does the C# factory pattern require an upcast? I want God in class library
I have a generic factory which caches an instance before return it (simplified code):
I want to create an delegate based export for an interface which is called
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert

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.