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 mentioned above. This class is responsible for retrieving data from a data source, which can 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 by using the DAO class.

summary

intent

Separate data access logic from business logic, and encapsulate data access operations in a dedicated class.

Main issues solved

  • Solve the problem of tight coupling between business logic and data access logic, improving code maintainability and reusability.

Use Case

  • When the application's data access logic needs to be centrally managed in order to simplify business logic and facilitate data operations.

Implementation Approach

  • Define DAO interface: Declares methods for data access operations.
  • Implement DAO class: Implement the DAO interface, encapsulating all access logic to the data source (such as a database).
  • Data Transfer Object (DTO): Optional, used to encapsulate data retrieved from the data source.

Key code

  • DAO Interface: Declares methods for data access and operations.
  • DAO Implementation: Provides a concrete implementation of the DAO interface, containing database access code.

Application example

  • User management application: The user DAO class is responsible for interacting with the database, managing CRUD operations for user data.

Advantages

  1. Separation of Concerns: Separate data access logic from business logic, reducing system coupling.
  2. Easy to maintain: Centralizes data access logic for easy maintenance and updates.
  3. Extensibility: When changing the data source or modifying data access logic, 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 suggestions

  • When the application needs to interact with multiple data sources, or the data access logic is relatively complex, use the DAO pattern.

Notes

  • DAO classes should be as simple as possible, containing only data access logic and no business logic.

Several main roles included

  1. DAO interface (DAO Interface):

    • Declares methods for data access and operations.
  2. DAO Implementation:

    • Implement the DAO interface, encapsulating all access logic to the data source.
  3. Data Transfer Object (DTO) (optional):

    • Encapsulate data retrieved from the data source for data transfer between the business logic layer and the data access layer.
  4. Business Logic Layer (Business Logic Layer):

    • Use the DAO interface to interact with the data source, without directly depending on the specific implementation of data access.
  5. Data Source:

    • Database or other persistent storage, with which DAO implementations interact to access and store data.

The Data Access Object Pattern helps keep an application clean and manageable by providing a clear data access layer.


Implementation

We will create an object that serves as a model object or value object,StudentObject.StudentDaoIs the Data Access Object interface.StudentDaoImplwhich is an entity class that implements the Data Access Object interface.DaoPatternDemo, our demo class usesStudentDaoto demonstrate the usage of the Data Access Object Pattern.

数据访问对象模式的 UML 图

Step 1

Create 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

Creates 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

Creates entity classes that implement the above interface.

StudentDaoImpl.java

import java.util.ArrayList; import java.util.List; public class StudentDaoImpl implements StudentDao { //Lists are 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 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

UsageStudentDaoto demonstrate the usage 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 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 and output the result:

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