Senin, 16 Juni 2014

Data Access Object (DAO) design pattern in Java - Tutorial Example




Data Access Object or DAO design pattern is a popular design pattern to
implement persistence layer of Java application. DAO pattern is based on abstraction
and encapsulation
design principles and shields rest of application from any change on
persistence layer e.g. change of database from
Oracle to MySQL, change of
persistence technology e.g. from File System to Database. For example if you
are authenticating user using relational database and later your company wants
to use LDAP
to perform authentication
. If you are using DAO design pattern to access database, it would be relatively safe
as you only need to make change on Data Access Layer. DAO design pattern also
keeps coupling low between different parts of application. By using DAO design
pattern your View Layer is completely independent to DAO layer and only Service
layer has dependency on it which is also abstracted by using DAO interface. You
can further use Generics
to template your DAO layer. If you are using Spring than you can leverage
JdbcTemplate for
performing JDBC calls which saves lot of boiler plate coding. Using DAO pattern
to access database is one of the JDBC
best practices
to follow.







What is Data Access Object (DAO) pattern in Java



Data access object design patter or DAO pattern in Java with ExampleIn short Data Access Object or DAO design pattern is way to reduce
coupling between Business logic and Persistence logic. Application business
logic often needs domain objects which is persisted in either Database, File
System or any other persistence storage. DAO pattern allows you to encapsulate
code for performing CRUD operation against persistence from rest of
application. Which means any change on persistence logic will not affect other
layers of application which is already tested. DAO pattern enables application
to cope with any change in database provider or persistence technology. In next
section we will What are the main benefits of using DAO design pattern in Java
application.





Benefits
of using DAO design pattern


DAO or Data Access Object design pattern is good example of abstraction
and encapsulation object
oriented principles
. It separates persistence logic is a separate layer
called Data access layer which enable application to react safely on change in
Persistence mechanism. For example if you shift from File based persistence
mechanism to Database, your change will be limited to data access layer and
won't impact Service layer or domain Objects. Data Access Object or DAO pattern
is pretty much standard in Java application being it core Java, web application
or enterprise application. Following are couple of more benefits of using DAO
pattern in Java application :





1) DAO design pattern allows JUnit
test
to run faster as it allows to create Mock and avoid connecting to
database to run tests. It improves testing because its easy to write test with
Mock objects, rather than an Integration test with database. In case of any
issues while running Unit test, you only need to check code and not database.
Also shields with database
connectivity and environment issues
.





2) Since DAO pattern is based on interface, it also promotes Object
oriented design principle "programming
for interface than implementation"
which results in flexible and
quality code.





DAO design pattern Example



In the core of Data Access Object or DAO pattern is a Java
interface
, which defines various method to perform CRUD operation e.g.
Create, Read, Update and Delete. Based
upon your application back-end technology you can create different
implementation of this interface e.g.
JdbcDAOImpl to connect
database using JDBC,
HibernateDAOImple to use hibernate or FileDAOImpl if you are
using File system for persistence. Service layer which uses this Data Access
Object will use interface to interact with Data access layer. Here is how a
typical DAO Interface look like:






public interface
AccountDAO{

   public boolean save(Account
account);

   public boolean update(Account
account);

   public boolean findByAccountNumber(int
accountNumber);

   public boolean delete(Account
account);



}






it defines various method to perform CRUD operation. Now you can create
different implementation of this
AccountDAO interface
e.g.
JdbcAccountDAOImpl or HibernateAccountDAOImpl. Your JdbcAccountDAOImpl will use
JDBC API or Spring
JdbcTemplate along with SQL
Queries
to perform CRUD operations.





That's all on what is Data Access Object or DAO pattern in Java
and what are benefits of using DAO pattern in Java application. We have also
see a simple example of How to implement
Data Access Object pattern
with
AccountDAO interface.
It's standard and one of the standard JDBC
practices
to use DAO pattern to create persistent layer in Java
application.





Other Java design pattern tutorials from Javarevisited blog





























Source:http://javarevisited.blogspot.com/2013/01/data-access-object-dao-design-pattern-java-tutorial-example.html

Tidak ada komentar:

Posting Komentar