why use daoFactory

Posted on 2006-04-18
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

    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

    LVL 3

    Expert Comment


    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
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Java 1603 Error 2 26
    Logs to delete in Windows VCenter server ? 11 87
    countAbc challenge 9 35
    @SBGen Method 3 14
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now