Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Data access object mode


May 27, 2021 Design mode


Table of contents


Data access object mode

Data Access Object Pattern or DAO mode is used to separate low-level data access APIs or operations from advanced business services. The following are participants in the data access object pattern.

  • Data Access Object Interface - This interface defines standard operations to be performed on a model object.
  • Data Access Object concrete class - This class implements the interfaces mentioned above. This class is responsible for getting data from a data source that can be either a database, xml, or some other storage mechanism.
  • Model Object/Value Object - This object is a simple POJO that contains the get/set method to store data retrieved by using the DAO class.

Realize

We'll create a Student object that is those that are model objects or numeric objects. o is the data provider interface. s an entity class that implements the data access object interface. DaoPattern Demo, our demo class uses StudentDao to demonstrate the use of data access object patterns.

Data access object mode

Step 1

Create a numeric 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 an entity class that implements the above interface.

StudentDaoImpl.java

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

public class StudentDaoImpl implements StudentDao {
    
   //列表是当作一个数据库
   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");
   }

   //从数据库中检索学生名单
   @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 data access object patterns.

CompositeEntityPatternDemo.java

public class DaoPatternDemo {
   public static void main(String[] args) {
      StudentDao studentDao = new StudentDaoImpl();

      //输出所有的学生
      for (Student student : studentDao.getAllStudents()) {
         System.out.println("Student: [RollNo : "
            +student.getRollNo()+", Name : "+student.getName()+" ]");
      }


      //更新学生
      Student student =studentDao.getAllStudents().get(0);
      student.setName("Michael");
      studentDao.updateStudent(student);

      //获取学生
      studentDao.getStudent(0);
      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 ]