Design Pattern

Data Access Object (DAO) – Core J2EE Patterns

The data access object in a computer software which is as an object which is responsible for providing abstract interface for communication to a specific form of database. Through the method of mapping, the app is able to call the persistence layer and the DAO then provides a certain type of data operations. You don’t need to expose what the database actually contains. This segregation is able to support the Single responsibility principle. It splits the need for the app in terms of data access from how can these needs be fulfilled with certain database schema, DBMS, etc. These needs can be domain specific or data types which is the public interface of the data access object.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

Data Access Object Design Pattern

The data access object design pattern is applicable to a large number of programming languages along with the same number of types of software which have persistence needs and a large number of types of databases. It associates with the Java EE apps along with the relational databases.

The objects of data access are comparatively simple to use and it stands as a separation between the most important two parts of the app, which is able to but should not know about each other. This evolves independently and frequently. The alteration in the business logic tends to depend on DAO interface also the alteration to the persistence logic cannot affect the DAO clients. But, this is only until, the interface is correctly and firmly implemented. The remaining part of the app does not have access to the entire details of the storage. You don’t need to modify the entire app to implement an alteration to the persistence mechanism. You can only modify a part of DAO implementation to do so.

Sample Implementation

Let’s see the sample implementation of this design pattern.

Step 1: Let’s create a Value Object.

Employee.java

/**
 * 
 */
package com.doj.patterns.j2ee.dataaccessobject;

/**
 * @author Dinesh.Rajput
 *
 */
public class Employee {
	
	private String name;
	private int empNo;
	
	public Employee(String name, int empNo) {
		super();
		this.name = name;
		this.empNo = empNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmpNo() {
		return empNo;
	}
	public void setEmpNo(int empNo) {
		this.empNo = empNo;
	}
	
}

Step 2: Let’s create Data Access Object Interface.

EmployeeDao.java

/**
 * 
 */
package com.doj.patterns.j2ee.dataaccessobject;

import java.util.List;

/**
 * @author Dinesh.Rajput
 *
 */
public interface EmployeeDao {
	List getAllEmployees();
	Employee getEmployee(int empNo);
	void updateEmployee(Employee employee, int index);
	void deleteEmployee(Employee employee);
}

Step 3: Let’s create concrete class implementing above interface.

EmployeeDaoImpl.java

/**
 * 
 */
package com.doj.patterns.j2ee.dataaccessobject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dinesh.Rajput
 *
 */
public class EmployeeDaoImpl implements EmployeeDao {
	
	 List employees;
	 
	 
	public EmployeeDaoImpl() {
		super();
		employees = new ArrayList<>();
		Employee employee1 = new Employee("Dinesh",100);
		Employee employee2 = new Employee("Arnav", 111);
		employees.add(employee1);
		employees.add(employee2);	
	}

	@Override
	public List getAllEmployees() {
		 return employees;
	}

	@Override
	public Employee getEmployee(int empNo) {
		return employees.get(empNo);
	}

	@Override
	public void updateEmployee(Employee employee, int index) {
		employees.get(index).setName(employee.getName());
		System.out.println("Employee: Emp No " + employee.getEmpNo() + ", updated in the database");
	   
	}

	@Override
	public void deleteEmployee(Employee employee) {
		employees.remove(employee.getEmpNo());
		System.out.println("Employee: Emp No " + employee.getEmpNo() + ", deleted from database");
	}

}

Step 4: Let’s create a demo class and use the EmployeeDao to demonstrate Data Access Object pattern usage.

DaoPatternDemo.java

/**
 * 
 */
package com.doj.patterns.j2ee.dataaccessobject;

/**
 * @author Dinesh.Rajput
 *
 */
public class DaoPatternDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EmployeeDao employeeDao = new EmployeeDaoImpl();

	      //print all Employees
	      for (Employee employee : employeeDao.getAllEmployees()) {
	         System.out.println("Employee: [Emp No : " + employee.getEmpNo() + ", Name : " + employee.getName() + " ]");
	      }

	      //update Employee
	      Employee employee = employeeDao.getAllEmployees().get(0);
	      employee.setName("Anamika");
	      employeeDao.updateEmployee(employee, 0);

	      //get the Employee
	      employeeDao.getEmployee(0);
	      System.out.println("Employee: [Emp No : " + employee.getEmpNo() + ", Name : " + employee.getName() + " ]");		
	}

}

Step 5: Let’s run this demo class and verify the output.

Employee: [Emp No : 100, Name : Dinesh ]
Employee: [Emp No : 111, Name : Arnav ]
Employee: Emp No 100, updated in the database
Employee: [Emp No : 100, Name : Anamika ]

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago