设计模式笔记 – 数据访问对象模式
数据访问对象模式(Data Access Object Pattern)或 DAO 模式用于把低级的数据访问 API 或操作从高级的业务服务中分离出来。以下是数据访问对象模式的参与者。
- 数据访问对象接口(Data Access Object Interface) – 该接口定义了在一个模型对象上要执行的标准操作。
- 数据访问对象实体类(Data Access Object concrete class) – 该类实现了上述的接口。该类负责从数据源获取数据,数据源可以是数据库,也可以是 xml,或者是其他的存储机制。
- 模型对象/数值对象(Model Object/Value Object) – 该对象是简单的 POJO,包含了 get/set 方法来存储通过使用 DAO 类检索到的数据。
示例
我们将创建一个作为模型对象或数值对象的 Student 对象。StudentDao 是数据访问对象接口。StudentDaoImpl 是实现了数据访问对象接口的实体类。DaoPatternDemo,我们的演示类使用 StudentDao 来演示数据访问对象模式的用法。
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;
}
}
创建数据访问对象接口
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);
}
创建实现了上述接口的实体类
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("Studnet: 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");
}
}
使用 StudentDao 来演示数据访问对象模式的用法
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);
student = studentDao.getStudent(0);
System.out.println("Student: [RollNo: "+student.getRollNo()+", Name: "+student.getName()+"]");
}
}
Python 实现
import abc
from typing import Tuple
class Student:
def __init__(self, name: str, roll_no: int):
self.name = name
self.roll_no = roll_no
def get_name(self) -> str:
return self.name
def set_name(self, name: str):
self.name = name
def get_roll_no(self) -> int:
return self.roll_no
def set_roll_no(self, roll_no: int):
self.roll_no = roll_no
class StudentDao(abc.ABC):
@abc.abstractmethod
def get_all_students(self) -> Tuple[Student, ...]: ...
@abc.abstractmethod
def get_student(self, roll_no: int) -> Student: ...
@abc.abstractmethod
def update_student(self, student: Student): ...
@abc.abstractmethod
def delete_student(self, student: Student): ...
class StudentDaoImpl(StudentDao):
def __init__(self):
self.students = []
student1 = Student("Robert", 0)
student2 = Student("John", 1)
self.students.append(student1)
self.students.append(student2)
def delete_student(self, student: Student):
self.students.remove(student)
print(f"Student: Roll No {student.get_roll_no()}, deleted from database")
def get_all_students(self):
return self.students
def get_student(self, roll_no: int):
return self.students[roll_no]
def update_student(self, student: Student):
self.students[student.get_roll_no()].set_name(student.get_name())
print(f"Student: Roll No {student.get_roll_no()}, updated in the database")
if __name__ == "__main__":
student_dao = StudentDaoImpl()
for student in student_dao.get_all_students():
print(f"Student: [RollNo: {student.get_roll_no()}, Name: {student.get_name()}]")
student = student_dao.get_all_students()[0]
student.set_name("Michael")
student_dao.update_student(student)
student = student_dao.get_student(0)
print(f"Student: [RollNo: {student.get_roll_no()}, Name: {student.get_name()}]")