The data Access object pattern, or the DAO pattern, is used to separate low-level data access APIs or operations from advanced business services. The following are participants in the data Access object pattern.
- data Access Object Interface (Interface) -this interface defines the standard operations to be performed on a model object.
- The data Access Object entity class (concrete Class) -This class implements the interface described above. This class is responsible for getting data from a data source, which can be a database, or XML, or other storage mechanism.
- model Object/value Object/Numeric Object -The object is a simple POJO that contains the Get/set method to store the data retrieved by using the DAO class.
Realize
We will create a Student object as a model object or a numeric object. Studentdao is the data Access object interface. Studentdaoimpl is an entity class that implements the interface of a data access object. Daopatterndemo, our demo class uses Studentdao to demonstrate the use of the data Access object pattern.
Step 1
Creates a numeric object.
Public classStudent {PrivateString name; Private intRollno; Student (String name,intRollno) { This. Name =name; This. Rollno =Rollno; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetrollno () {returnRollno; } Public voidSetrollno (intRollno) { This. Rollno =Rollno; }}
Step 2
Creates a data Access object interface.
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
Create an entity class that implements the above interfaces.
Importjava.util.ArrayList;Importjava.util.List; Public classStudentdaoimplImplementsStudentdao {//list is treated as a databaseList<student>students; PublicStudentdaoimpl () {Students=NewArraylist<student>(); Student Student1=NewStudent ("Robert", 0); Student Student2=NewStudent ("John", 1); Students.add (STUDENT1); Students.add (Student2); } @Override Public voiddeletestudent (Student Student) {students.remove (Student.getrollno ()); System.out.println ("Student:roll No" +Student.getrollno ()+ ", deleted from database"); } //Retrieving student lists from the database@Override PublicList<student>getallstudents () {returnstudents; } @Override PublicStudent Getstudent (intRollno) { returnStudents.get (Rollno); } @Override Public voidupdatestudent (Student Student) {students.get (Student.getrollno ()). SetName (Student.getname ()); System.out.println ("Student:roll No" +Student.getrollno ()+ ", updated in the database"); }}
Step 4
Use Studentdao to demonstrate the use of the data Access object pattern.
Public classDaopatterndemo { Public Static voidMain (string[] args) {Studentdao Studentdao=NewStudentdaoimpl (); //output of all students for(Student student:studentDao.getAllStudents ()) {System.out.println ("Student: [Rollno:" +student.getrollno () + ", Name:" +student.getname () + "]"); } //Update StudentStudent Student =studentdao.getallstudents (). Get (0); Student.setname ("Michael"); Studentdao.updatestudent (student); //Get StudentsStudentdao.getstudent (0); System.out.println ("Student: [Rollno:" +student.getrollno () + ", Name:" +student.getname () + "]"); }}
Step 5
Verify the output.
Student: [rollno:0100, Name:michael]
"Design mode" Data Access object mode