0% found this document useful (0 votes)
225 views

2.DAO Factory Design Pattern

The DAO Factory design pattern allows an application to work with multiple databases. It extends the DAO design pattern by adding a factory class that constructs DAO objects specific to a given database. This factory class contains logic to return the appropriate DAO based on the database name provided. This pattern helps change the database software with minimal changes to the persistence logic by abstracting the DAO instantiation through the factory. The example shows abstract DAO classes for Oracle and MS Access that extend a base DAO class. The factory returns the correct DAO implementation based on the database name.

Uploaded by

kasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

2.DAO Factory Design Pattern

The DAO Factory design pattern allows an application to work with multiple databases. It extends the DAO design pattern by adding a factory class that constructs DAO objects specific to a given database. This factory class contains logic to return the appropriate DAO based on the database name provided. This pattern helps change the database software with minimal changes to the persistence logic by abstracting the DAO instantiation through the factory. The example shows abstract DAO classes for Oracle and MS Access that extend a base DAO class. The factory returns the correct DAO implementation based on the database name.

Uploaded by

kasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA Means DURGA SOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 1
JAVA Means DURGA SOFT

DAO Factory Design Pattern

The DAO Factory design pattern is nothing but an extension to DAO Design Pattern.
Generally we can use DAO Design Pattern in order to separate persistence logic from other
logics of the application. Here implemented DAO class/ component contains persistence logic
with respective to a single a database. If you want to work with multiple databases then go
through the DAO Factory design pattern.

Def: DAO Factory design pattern is a class or component, which contains logic of constructing
and returning one database software specific DAO class object based on the database software
name we are provided i.e. DAO Factory design pattern is a combination of both Factory Pattern
and DAO Design Pattern.

This design pattern helps the programmer to change the database software of the
project with minimum changes done in JDBC persistence logic.

Ex:

//BComp1BaseDAO.java

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 2
JAVA Means DURGA SOFT

public abstract class BComp1BaseDAO

public abstract Connection getConnection();

public abstract void closeConnection();

public abstract int insert(-,-,-);

public abstract List fetch(-,-);

public static BComp1BaseDAO chooseDAO(String dbname)

if(dbname.equals("oracle"))

return new Comp1OracleDAO();

else if(dbname.equals("MsAccess"))

return new Comp1MsAccessDAO();

else

throw new Exception("Please Provide Existed D/B Name");

return null;

};

public class Comp1OracleDAO extends BComp1BaseDAO

{
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 3
JAVA Means DURGA SOFT

public Connection getConnection()

----

----

public int insert(-,-,-)

----

----

public List fetch(-,-)

----

----

public void closeConnection()

----

----

public class Comp1MsAccessDAO extends BComp1BaseDAO

public Connection getConnection()

----
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 4
JAVA Means DURGA SOFT

----

public int insert(-,-,-)

----

----

public List fetch(-,-)

----

----

public void closeConnection()

----

----

public class BComp1

BComp1BaseDAO dao=null;

public BComp1()

dao=BComp1BaseDAO.chooseDAO("oracle");

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 5
JAVA Means DURGA SOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 6
JAVA Means DURGA SOFT

public void bm1()

dao.getConnection();

---- // write some business logic

----

// implement some persistence logic

dao.insert(-,-,-);

dao.closeConnection();

public class BComp2

BComp1BaseDAO dao=null;

public BComp1()

dao=BComp1BaseDAO.chooseDAO("MsAccess");

public void bm2()

dao.getConnection();

---- // write some business logic

----

// implement some persistence logic

dao.insert(-,-,-);

dao.closeConnection();
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 7
JAVA Means DURGA SOFT

DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page 8

You might also like