Data Access Object Pattern
The Data Access Object Pattern, or DAO Pattern, is used to separate low-level data access APIs or operations from high-level business services. The following are the participants of the Data Access Object Pattern.
- Data Access Object Interface - This interface defines the standard operations to be performed on a model object.
- Data Access Object concrete class - This class implements the interface above. It is responsible for fetching data from a data source, which can be a database, xml, or another storage mechanism.
- Model Object/Value Object - This object is a simple POJO containing get/set methods to store the data retrieved using the DAO class.
Summary
Intent
Separate the data access logic from the business logic and encapsulate data access operations in a dedicated class.
Main problem solved
- Solve the problem of tight coupling between business logic and data access logic, and improve the maintainability and reusability of the code.
Use Cases
- When you need to centralize the data access logic of the application in order to simplify the business logic and make data operations easier.
Implementation
- Define the DAO interface: declares the methods for data access operations.
- Implement the DAO class: implements the DAO interface and encapsulates all access logic to the data source (such as a database).
- Data Transfer Object (DTO): optional, used to encapsulate the data retrieved from the data source.
Key code
- DAO interface: declares the methods for data access and manipulation.
- DAO implementation: provides the concrete implementation of the DAO interface, including the code for database access.
Application Example
- User management application: the user DAO class interacts with the database and manages the CRUD operations of user data.
Advantages
- Separation of concerns: separates the data access logic from the business logic, reducing the coupling of the system.
- Easy to Maintain: data access logic is centrally managed, making it easy to maintain and update.
- Extensibility: when the data source is changed or the data access logic is modified, the business logic layer is not affected.
Disadvantages
- May increase complexity: for simple applications, introducing the DAO pattern may add an extra layer of abstraction.
Usage recommendation
- Use the DAO pattern when the application needs to interact with multiple data sources or when the data access logic is relatively complex.
Notes
- The DAO class should be kept as simple as possible, containing only data access logic and no business logic.
Main roles included
-
DAO Interface:
-
DAO Implementation:
-
Data Transfer Object (DTO) (optional):
-
Business Logic Layer (Business Logic Layer):
-
Data Source:
The Data Access Object Pattern helps keep an application clean and manageable by providing a clear data access layer.
Create a class that implements
We will create a class that acts as the model object or value object. Student object.StudentDao is the Data Access Object interface.StudentDaoImpl is the concrete class that implements the Data Access Object interface.DaoPatternDemo, and our demo class uses the StudentDao to demonstrate the use of the Data Access Object Pattern.
Step 1
Create the value object.
Student.java
public
class
Student
{
private
String
name
;
private
int
rollNo
;
Student
(
String
name
,
int
rollNo
)
{
this
.
name
=
name
;
this
.
rollNo
=
rollNo
;
}
public
String
getName
(
)
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
int
getRollNo
(
)
{
return
rollNo
;
}
public
void
setRollNo
(
int
rollNo
)
{
this
.
rollNo
=
rollNo
;
}
}
Step 2
Create the Data Access Object interface.
StudentDao.java
import
java
.
util
.
List
;
public
interface
StudentDao
{
public
List
<
Student
getAllStudents
(
);
public
Student
getStudent
(
int
rollNo
)
;
public
void
updateStudent
(
Student
student
)
;
public
void
deleteStudent
(
Student
student
)
;
}
Step 3
Create the concrete class that implements the interface above.
StudentDaoImpl.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
StudentDaoImpl
implements
StudentDao
{
//
The list is treated as a database
List
<
Student
students;
public
StudentDaoImpl
(
)
{
students
=
new
ArrayList
<
Student
(
);
Student
student1
=
new
Student
(
"
Robert
"
,
0
)
;
Student
student2
=
new
Student
(
"
John
"
,
1
)
;
students
.
add
(
student1
)
;
students
.
add
(
student2
)
;
}
@
Override
public
void
deleteStudent
(
Student
student
)
{
students
.
remove
(
student
.
getRollNo
(
)
)
;
System
.
out
.
println
(
"
Student: Roll No
"
student
.
getRollNo
(
)
"
, deleted from database
"
)
;
}
//
Retrieve the list of students from the database
@
Override
public
List
<
Student
getAllStudents
(
)
{
return
students;
}
@
Override
public
Student
getStudent
(
int
rollNo
)
{
return
students
.
get
(
rollNo
)
;
}
@
Override
public
void
updateStudent
(
Student
student
)
{
students
.
get
(
student
.
getRollNo
(
)
)
.
setName
(
student
.
getName
(
)
)
;
System
.
out
.
println
(
"
Student: Roll No
"
student
.
getRollNo
(
)
"
, updated in the database
"
)
;
}
}
Step 4
Use StudentDao to demonstrate the use of the Data Access Object Pattern.
DaoPatternDemo.java
public
class
DaoPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
StudentDao
studentDao
=
new
StudentDaoImpl
(
)
;
//
Output all students
for
(
Student
student
:
studentDao
.
getAllStudents
(
)
)
{
System
.
out
.
println
(
"
Student: [RollNo :
"
student
.
getRollNo
(
)
"
, Name :
"
student
.
getName
(
)
"
]
"
)
;
}
//
Update a student
Student
student
=
studentDao
.
getAllStudents
(
)
.
get
(
0
)
;
student
.
setName
(
"
Michael
"
)
;
studentDao
.
updateStudent
(
student
)
;
//
Get a student
studentDao
.
getStudent
(
0
)
;
System
.
out
.
println
(
"
Student: [RollNo :
"
student
.
getRollNo
(
)
"
, Name :
"
student
.
getName
(
)
"
]
"
)
;
}
}
Step 5
Run the program, output result:
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]