why use daoFactory

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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
not a lot.
using getDao() would not require the dao classes to be available at compile time, thats about the only difference.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:

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.

Tommy BraasCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.