[Last Call] Learn how to a build a cloud-first strategyRegister Now


why use daoFactory

Posted on 2006-04-18
Medium Priority
Last Modified: 2006-11-18
In  our java web application  we are   using  this method for getting an instanse of a Dao Class by passing the name of the class as parameter string to this method and this method  returns an object which we cast  to spacific Dao Class.

 public BaseDAO getDAO(String sDaoClass)
        BaseDAO dao = null;
            dao = (BaseDAO)Class.forName(sDaoClass).newInstance();
        catch(ClassNotFoundException clsEx)
        catch(InstantiationException initEx)
        catch(IllegalAccessException iaEx)
        return dao;

Now i want to know why this pattern is used to get the instanse of the class.

we can also get the object of a class by calling the constructor :
                    specificDaoClass  referenceName = new  specificDaoClass ();
then why we use this(getDAO(String)) to get instanse of classes.

what is the difference between these two approach,
is there any drawbacks ,limitations or benifits while using these approaches
i mean i m totally confused about these approaches
please give ur comments,share ur knowldge,  not just  google reference.
please help
Thanks in Advance
Question by:riteinfotech
LVL 92

Accepted Solution

objects earned 400 total points
ID: 16476821
not a lot.
using getDao() would not require the dao classes to be available at compile time, thats about the only difference.
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 600 total points
ID: 16476877

Expert Comment

ID: 16478537

Wanna a know a problem. Say you do not want to package all the class files in your ear/war/jar(maybe you donot use it but you do use it somewhere in some other ear/jar/war) or maynot even want to compile them.

Now you have a common DAO, with the (BaseDAO)Class.forName(sDaoClass).newInstance() approach your code works but with new specificDaoClass () the code bombs.

I am sure whoever used such a code must have had a very bad time with some existing code, to use this approach. When you look at the bigger picture it would help.

LVL 14

Expert Comment

by:Tommy Braas
ID: 16479348
One big advantage with the approach above is that a new version of a DAO can be subsituted in without ever needing to change, or recompile any code. This is especially true if the DAO declarations are interfaces. Compare to JDBC drivers for instance. You can replace a driver for a database by replacing the jar which contains the driver. No need to recompile any code.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question