Data Access Object Pattern
Data Access Object Model (Data Access Object Pattern) or DAO pattern for the low-level data access API or separated from the operation of advanced business services. The following is a participant in the Data Access Object pattern.
- Interface Data Access Objects (Data Access Object Interface) - This interface defines the standard operations on a model object to be executed.
- Data Access Objects entity classes (Data Access Object concrete class) - This class implements the above interfaces.This class is responsible for getting data from the data source, the data source can be a database, it can be xml, or other storage mechanism.
- Model Object / value objects (Model Object / Value Object) - The object is simple POJO, includes get / set methods to store by using DAO classes to retrieve data.
achieve
We will create aStudentobject model object or value object as.StudentDaodata access object interface.StudentDaoImplentity class is to achieve a data access object interface.DaoPatternDemo,our demonstration class to demonstrate the use ofStudentDaoData Access Object pattern of usage.
step 1
Create a value object.
Student.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; } }
Step 2
Create a data access object interface.
StudentDao.java
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 entity class implements the above interfaces.
StudentDaoImpl.java
import java.util.ArrayList; import java.util.List; public class StudentDaoImpl implements StudentDao { // List is a database as a 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 ( "Student: Roll No" + student.getRollNo () + ", Deleted from database"); } // Retrieve the list of students from the 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"); } }
Step 4
StudentDaoto demonstrate the use of the Data Access Object pattern of usage.
CompositeEntityPatternDemo.java
public class DaoPatternDemo { public static void main (String [] args) { StudentDao studentDao = new StudentDaoImpl (); // Output all students for (Student student: studentDao.getAllStudents ()) { System.out.println ( "Student: [RollNo:" + Student.getRollNo () + ", Name:" + student.getName () + "]"); } // Update Student Student student = studentDao.getAllStudents () get (0).; student.setName ( "Michael"); studentDao.updateStudent (student); // Get students studentDao.getStudent (0); System.out.println ( "Student: [RollNo:" + Student.getRollNo () + ", Name:" + student.getName () + "]"); } }
Step 5
Verify output.
Student: [RollNo: 0, Name: Robert] Student: [RollNo: 1, Name: John] Student: Roll No 0, updated in the database Student: [RollNo: 0, Name: Michael]