Design Pattern 简明教程

Data Access Object Pattern

数据访问对象模式或 DAO 模式用于将低级别数据访问 API 或操作从高级业务服务中分离。以下是数据访问对象模式中的参与者。

  1. Data Access Object Interface - 此接口定义要在模型对象上执行的标准操作。

  2. Data Access Object concrete class - 此类实现了上述接口。此类负责从数据源获取数据,此数据源可以是数据库/xml 或任何其他存储机制。

  3. Model Object or Value Object - 此对象是包含 get/set 方法的简单 POJO,用于存储使用 DAO 类检索的数据。

Implementation

我们将创建一个作为模型或值对象的 Student 对象。StudentDao 是数据访问对象接口。StudentDaoImpl 是实现数据访问对象接口的具体类。我们的演示类 DaoPatternDemo 将使用 StudentDao 来演示数据访问对象模式的使用。

dao pattern uml diagram

Step 1

创建值对象。

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

创建数据访问对象接口。

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

创建实现上述接口的具体类。

StudentDaoImpl.java

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

   //retrive 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

使用 StudentDao 来演示数据访问对象模式用法。

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 the student
      studentDao.getStudent(0);
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
   }
}

Step 5

验证输出。

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