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

x
?
Solved

why use daoFactory

Posted on 2006-04-18
4
Medium Priority
?
891 Views
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;
        try
        {
            dao = (BaseDAO)Class.forName(sDaoClass).newInstance();
        }
        catch(ClassNotFoundException clsEx)
        {
            logger.error(clsEx.getMessage());
        }
        catch(InstantiationException initEx)
        {
            logger.error(initEx.getMessage());
        }
        catch(IllegalAccessException iaEx)
        {
            logger.error(iaEx.getMessage());
        }
        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
0
Comment
Question by:riteinfotech
4 Comments
 
LVL 92

Accepted Solution

by:
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.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 600 total points
ID: 16476877
0
 
LVL 3

Expert Comment

by:el_dios
ID: 16478537
Hi,

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.

Cheers!
0
 
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.
0

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