Design Patterns - SPLessons

Data Access Object Pattern

Home > Lesson > Chapter 32
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Data Access Object Pattern

Data Access Object Pattern

shape Description

Data Access Object Pattern - Utilize a Data Access Object (DAO) to extract and typify all permision to the information source. The DAO deals with the association with the information source to get and store data. The DAO actualizes the entrance instrument required to work with the information source. The information source could be a relentless store like a RDBMS, an outside administration like a B2B trade, a storehouse like a LDAP database, or a business administration got to by means of CORBA Internet Inter-ORB Protocol (IIOP) or low-level attachments. The business segment that depends on the DAO utilizes the less difficult interface uncovered by the DAO for its customers. The DAO totally conceals the information source execution subtle elements from its customers. Since the interface uncovered by the DAO to customers does not change when the basic information source execution transforms, this example permits the DAO to adjust to various stockpiling plans without influencing its customers or business segments. Basically, the DAO goes about as a connector between the segment and the information source.

shape Example

Following is an example to understand the Data Access Object Pattern. Student.java [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; } }[/java] Data Access Object Pattern - Here the developer has taken two data members such as name and rollNo and also performed SET and GET methods on these members. StudentDao.java [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); }[/java] Data Access Object Pattern is the essential object of this example. The DataAccessObject abstracts the basic information access execution for the BusinessObject to empower straightforward access to the information source. The BusinessObject likewise assigns information load and store operations to the DataAccessObject. StudentDaoImpl.java [java]import java.util.ArrayList; import java.util.List; public class StudentDaoImpl implements StudentDao { //list is working as a database List<Student> students; public StudentDaoImpl(){ students = new ArrayList<Student>(); Student student1 = new Student("Splesson",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"); } //retrive 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"); } }[/java] The DataAccessObject may utilize a Transfer Object to return information to the customer. The DataAccessObject may likewise get the information from the customer in a Transfer Object to upgrade the information in the information source. DaoPatternDemo.java [java]public class DaoPatternDemo { public static void main(String[] args) { StudentDao studentDao = new StudentDaoImpl(); //print 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("Sachin"); studentDao.updateStudent(student); //get the student studentDao.getStudent(0); System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]"); } }[/java] This is the main class and code will be executed from here. A layer of DAOs makes it less demanding for an application to move to an alternate database execution. The business objects have no information of the fundamental information execution. Accordingly, the relocation includes changes just to the DAO layer. Further, if utilizing an industrial facility system, it is conceivable to give a solid manufacturing plant usage to each basic stockpiling execution. For this situation, relocating to an alternate stockpiling usage implies giving another plant execution to the application. Output will be as follows. [java] Student: [RollNo : 0, Name : Splesson ] Student: [RollNo : 1, Name : John ] Student: Roll No 0, updated in the database Student: [RollNo : 0, Name : Sachin ] [/java]

Summary

shape Key Points

  • Data Access Object Pattern Reduces Code Complexity in Business Objects
  • Data Access Object Pattern Centralizes All Data Access into a Separate Layer