<p>The <strong>data access object</strong> 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&#8217;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.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput"><br />
<img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://i0.wp.com/dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern.jpg?resize=243%2C300&;ssl=1" alt="Spring-5-Design-Pattern" width="243" height="300" /></a></div>
</div>
<h2>Data Access Object Design Pattern</h2>
<p>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.</p>
<div style="text-align: left;">
<blockquote>
<p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/context-object-design-pattern/" href="https://dineshonjava.com/context-object-design-pattern/">Service Activator Pattern</a></li>
<li><a title="https://dineshonjava.com/front-controller-design-pattern/" href="https://dineshonjava.com/front-controller-design-pattern/">Web Service Broker Pattern</a></li>
</ul>
</blockquote>
</div>
<p><img class="aligncenter wp-image-4069 size-full" src="https://dineshonjava.com/wp-content/uploads/2018/01/Data-Access-Object.gif" alt="Data Access Object" width="532" height="265" /></p>
<p>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&#8217;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.</p>
<h2>Sample Implementation</h2>
<p>Let&#8217;s see the sample implementation of this design pattern.</p>
<h3>Step 1: Let&#8217;s create a Value Object.</h3>
<p><strong>Employee.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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; 
	} 
	 
} 
 
</pre>
<h3>Step 2: Let&#8217;s create Data Access Object Interface.</h3>
<p><strong>EmployeeDao.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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); 
} 
 
</pre>
<h3>Step 3: Let&#8217;s create concrete class implementing above interface.</h3>
<p><strong>EmployeeDaoImpl.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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"); 
	} 
 
} 
 
</pre>
<h3>Step 4: Let&#8217;s create a demo class and use the EmployeeDao to demonstrate Data Access Object pattern usage.</h3>
<p><strong>DaoPatternDemo.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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() + " ]");		 
	} 
 
} 
 
</pre>
<h3>Step 5: Let&#8217;s run this demo class and verify the output.</h3>
<pre class="highlight">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 ] 
 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/transfer-object/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/service-activator/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4068'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…