Data Access Object Pattern
The Data Access Object Pattern, or DAO Pattern, is used to separate low-level data accessing API or operations from high-level business services. Below 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 above interface. It is responsible for retrieving data from a data source, which could be a database, XML, or other storage mechanisms.
Model Object/Value Object - This object is a simple POJO containing get/set methods to store data retrieved using the DAO class.
Implementation
We will create a Student object as a model or value object. StudentDao is the Data Access Object interface. StudentDaoImpl is the concrete class implementing the Data Access Object interface. DaoPatternDemo, our demo class, uses StudentDao to demonstrate the usage of the Data Access Object Pattern.
Step 1
Create a 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 a 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 a concrete class implementing the above interface.
StudentDaoImpl.java
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
// 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 student list 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 usage of the Data Access Object Pattern.
DaoPatternDemo.java
public class DaoPatternDemo {
public static void main(String[] args) {
StudentDao studentDao = new StudentDaoImpl();
// Print all students
for (Student student : studentDao.getAllStudents()) {
System.out.println("Student: [RollNo : "
+student.getRollNo()+", Name : "+student.getName()+" ]");
}
// Update student
Student student =studentDao.getAllStudents().get(0);
student.setName("Michael");
studentDao.updateStudent(student);
// Get student
studentDao.getStudent(0);
System.out.println("Student: [RollNo : "
+student.getRollNo()+", Name : "+student.getName()+" ]");
}
}
Step 5
Execute the program, output results:
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]