publicStudentDaoImpl(){ students = new ArrayList<Student>(); Student student1 = new Student("Robert",0); Student student2 = new Student("John",1); students.add(student1); students.add(student2); } @Override publicvoiddeleteStudent(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 publicvoidupdateStudent(Student student){ students.get(student.getRollNo()).setName(student.getName()); System.out.println("Student: Roll No " + student.getRollNo() +", updated in the database"); } }
$ javac -d . src/main/cn/twle/gof/DaoPatternDemo.java $ java cn.twle.gof.DaoPatternDemo Student: [RollNo : 0, Name : Robert ] Student: [RollNo : 1, Name : John ] Student: Roll No 0, updated in the database Student: [RollNo : 0, Name : Michael ]