Skip to main content
 首页 » 编程设计

Java 数据访问对象模式

2022年07月19日45EasonJim

1.数据访问对象模式简介

数据访问对象模式(Data Access Object Pattern)或 DAO 模式用于把低级的数据访问 API 或操作从高级的业务服务中分离出来。以下是数
据访问对象模式的参与者。

数据访问对象接口(Data Access Object Interface):该接口定义了在一个模型对象上要执行的标准操作。
数据访问对象实体类(Data Access Object concrete class):该类实现了上述的接口。该类负责从数据源获取数据,数据源可以是数据库,也可以是 xml,或者是其他的存储机制。
模型对象/数值对象(Model Object/Value Object):该对象是简单的 POJO,包含了 get/set 方法来存储通过使用 DAO 类检索到的数据。


2.示例Demo

import java.util.List; 
import java.util.ArrayList; 
 
interface StudentDao { 
    List<Student> getAllStudents(); 
    void deleteStudent(int number); 
    void updateStudent(int number, String name); 
    void addStudent(Student student); 
} 
 
class StudentDaoImpl implements StudentDao { 
    private List<Student> lists = new ArrayList<Student>(); 
 
    public List<Student> getAllStudents() { 
        return lists; 
    } 
    public void deleteStudent(int number) { 
        for (Student stu : lists) { 
            if (stu.getNumber() == number) { 
                /*注意java中删除链表中元素是remove而不是delete*/ 
                lists.remove(stu); 
                System.out.println("Delete student: name = " + stu.getName() + " number = " + stu.getNumber()); 
            } 
        } 
    } 
    public void updateStudent(int number, String name) { 
        for (Student stu : lists) { 
            if (stu.getNumber() == number) { 
                /*注意换行时“+”要写在一行的最前面而不是上一行的后面,不能使用连接符"\"*/ 
                System.out.println("Update student from: name = " + stu.getName() + " number = " + stu.getNumber() 
                    + "  to: name = " + name + " number = " + number); 
                stu.setName(name); 
            } 
        } 
    } 
    public void addStudent(Student student) { 
        lists.add(student); 
        System.out.println("Add student: name = " + student.getName() + " number = " + student.getNumber()); 
    } 
} 
 
class Student { 
    private String name; 
    private int number; 
 
    public Student(String name, int number) { 
        this.name = name; 
        this.number = number; 
    } 
 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
 
    public int getNumber() { 
        return number; 
    } 
    public void setNumber(int number) { 
        this.number = number; 
    } 
} 
 
public class DaoPatternDemo { 
    public static void main(String args[]) { 
        StudentDao dao = createStudentDao(); 
        dao.updateStudent(2, "kitty"); 
 
        System.out.println("All students are:"); 
        for (Student stu : dao.getAllStudents()) { 
            System.out.println("name = " + stu.getName() + " number = " + stu.getNumber()); 
        } 
    } 
 
    public static StudentDao createStudentDao() { 
        StudentDao dao = new StudentDaoImpl(); 
        dao.addStudent(new Student("James", 1)); 
        dao.addStudent(new Student("jane", 2)); 
        dao.addStudent(new Student("lucy", 3)); 
        return dao; 
    } 
}

参考:http://www.runoob.com/design-pattern/data-access-object-pattern.html


本文参考链接:https://www.cnblogs.com/hellokitty2/p/10746135.html