6.31. Data access object mode

发布时间 :2025-10-25 12:24:47 UTC      

The data access object pattern (Data Access Object Pattern) or DAO pattern is used to separate low-level data access API or operations from high-level business services. The following are the participants in 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 above interface. This class is responsible for fetching data from a data source, which can be a database, xml, or other storage mechanism.

  • 模型对象/数值对象(Model Object/Value Object) This object is a simple POJO that contains get/set methods to store the data retrieved by using the DAO class.

6.31.1. Realize

We will create a model object or a numeric object Student Object. StudentDao Is the data access object interface. StudentDaoImpl Is an entity class that implements the data access object interface. DaoPatternDemo Our demo class uses the StudentDao To demonstrate the use of the data access object pattern.

数据访问对象模式的 UML 图

6.31.2. Step 1

Create numeric objects.

Student.java

publicclassStudent{privateStringname;privateintrollNo;Student(Stringname,introllNo){this.name=name;this.rollNo=rollNo;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetRollNo(){returnrollNo;}publicvoidsetRollNo(introllNo){this.rollNo=rollNo;}}

6.31.3. Step 2

Create a data access object interface.

StudentDao.java

importjava.util.List;publicinterfaceStudentDao{publicList<Student>getAllStudents();publicStudentgetStudent(introllNo);publicvoidupdateStudent(Studentstudent);publicvoiddeleteStudent(Studentstudent);}

6.31.4. Step 3

Create an entity class that implements the above interface.

StudentDaoImpl.java

importjava.util.ArrayList;importjava.util.List;publicclassStudentDaoImplimplementsStudentDao{//列表是当作一个数据库List<Student>students;publicStudentDaoImpl(){students=newArrayList<Student>();Studentstudent1=newStudent("Robert",0);Studentstudent2=newStudent("John",1);students.add(student1);students.add(student2);}@OverridepublicvoiddeleteStudent(Studentstudent){students.remove(student.getRollNo());System.out.println("Student:
Roll No"+student.getRollNo()+", deleted from
database");}//从数据库中检索学生名单@OverridepublicList<Student>getAllStudents(){returnstudents;}@OverridepublicStudentgetStudent(introllNo){returnstudents.get(rollNo);}@OverridepublicvoidupdateStudent(Studentstudent){students.get(student.getRollNo()).setName(student.getName());System.out.println("Student:
Roll No"+student.getRollNo()+", updated in the database");}}

6.31.5. Step 4

Use StudentDao To demonstrate the use of the data access object pattern.

DaoPatternDemo.java

publicclassDaoPatternDemo{publicstaticvoidmain(String[]args){StudentDaostudentDao=newStudentDaoImpl();//输出所有的学生for(Studentstudent:studentDao.getAllStudents()){System.out.println("Student:
[RollNo :"+student.getRollNo()+", Name
:"+student.getName()+"]");}//更新学生Studentstudent=studentDao.getAllStudents().get(0);student.setName("Michael");studentDao.updateStudent(student);//获取学生studentDao.getStudent(0);System.out.println("Student:
[RollNo :"+student.getRollNo()+", Name :"+student.getName()+"]");}}

6.31.6. 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 ]
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.