Data Access Object Pattern

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services. Following are the participants in Data Access Object Pattern.

  • Data Access Object Interface - This interface defines the standard operations to be performed on a model object(s).

  • Data Access Object concrete class - This class implements above interface. This class is responsible to get data from a data source which can be database / xml or any other storage mechanism.

  • Model Object or Value Object - This object is simple POJO containing get/set methods to store data retrieved using DAO class.


We are going to create a Student object acting as a Model or Value Object.StudentDao is Data Access Object Interface.StudentDaoImpl is concrete class implementing Data Access Object Interface. DaoPatternDemo, our demo class, will use StudentDao to demonstrate the use of Data Access Object pattern.

Data Access Object Pattern UML Diagram

Step 1

Create Value Object.

public class Student {
   private String name;
   private int rollNo;

   Student(String name, int rollNo){ = name;
      this.rollNo = rollNo;

   public String getName() {
      return name;

   public void setName(String name) { = name;

   public int getRollNo() {
      return rollNo;

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;

Step 2

Create Data Access Object Interface.

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 concrete class implementing above interface.

import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
   //list is working as a database
   List<Student> students;

   public StudentDaoImpl(){
      students = new ArrayList<Student>();
      Student student1 = new Student("Robert",0);
      Student student2 = new Student("John",1);
   public void deleteStudent(Student student) {
      System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database");

   //retrive list of students from the database
   public List<Student> getAllStudents() {
      return students;

   public Student getStudent(int rollNo) {
      return students.get(rollNo);

   public void updateStudent(Student student) {
      System.out.println("Student: Roll No " + student.getRollNo() + ", updated in the database");

Step 4

Use the StudentDao to demonstrate Data Access Object pattern usage.

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);

      //get the student
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");		

Step 5

Verify the output.

Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]