Data Access Object Design Pattern
By: Daniel Liu
Tags: Java, Programming, Object Oriented, Backend Development, Design Patterns

Overview

Data Access Object or DAO is used to separate used low level from high level data access api. There are 3 parts to DAO:

Data Access Object Interface — The interface contains the operations that can be performed on the models.

— The interface contains the operations that can be performed on the models. Data Access Object Class — The class implements the interface. The class is what retrieves the data from the database or wherever the data is stored.

— The class implements the interface. The class is what retrieves the data from the database or wherever the data is stored. Model Object — The object is a simple Plain Old Java Object(POJO) with getters and setters to store data. To access the object you would use the DAO class.

Photo by Lukas Blazek on Unsplash

Implementation

Model Object

// Employee.java

public class Employee {

private String name;

private int id;



Employee(String name, int id){

this.name = name;

this.species = species;

}



public String getName() {

return this.name;

}



public void setName(String name) {

this.name = name;

}



public int getId() {

return this.id;

}



public void setId(int id) {

this.id = id;

}

}

Data Access Object Interface

// EmployeeDao.java

import java.util.List;



public interface PetDao {

public List<Employee> getAllEmployees();

public Employee getEmployee(int id);

public void updateEmployee(Employee employee);

public void deleteEmployee(Employee employee);

}

Data Access Object Class

// EmployeeDaoImpl.java

import java.util.ArrayList;

import java.util.List;



public class EmployeeDaoImpl implements EmployeeDao {



// list as a database

List<Employee> employees;



public EmployeeDaoImpl(){

employees = new ArrayList<Employee>();

Employee employees1 = new Employee("Mark",0);

Employee employees2 = new Employee("Bob",1);

employees.add(employees1);

employees.add(employees2);

} @Override

public void deleteEmployee(Employee employee) {

employees.remove(employee.getId());

System.out.println(employee.getId() + " deleted.");

}



@Override

public List<Employee> getAllEmployees() {

return this.employees;

}



@Override

public Employee getEmployee(int id) {

return employees.get(id);

}



@Override

public void updateEmployee(Employee employee) {

employees.get(employee.getId()).setName(employee.getName());

System.out.println(employee.getId() + " updated.");

}

}

Demonstration

// Demo.java

public class Demo {

public static void main(String[] args) {

EmployeeDao employeeDao = new EmployeeDaoImpl();



//print all employees

for (Employee employee : employeeDao.getAllEmployees()) {

System.out.println("Id: " + employee.getId() + ", Name: " + employee.getName());

}



// get employee

Employee employee = employeeDao.getEmployee(0);

employee.setName("Michael");

System.out.println("Id: " + employee.getId() + ", Name: " + employee.getName());

// update employee

employeeDao.updateEmployee(employee);

System.out.println("Id: " + employee.getId() + ", Name: " + employee.getName());

} >> Id: 0, Name: Mark

>> Id: 1, Name: Bob

>> Id: 0, Name: Mark

>> 0 updated.

>> Id: 0, Name: Michael

Conclusion

DAO is useful for when you need to change databases. For example, if you need to change from SQL to Mongo the only changes that need to be made is in the DAO layer. The separation of logic ensures that only the service layer depends on the DAO layer not the view. It is easier to write tests for individual components. DAO also emphasizes using interfaces which is part of OOP programming.