DAO Design Pattern
Tags: java, softwaredevelopment, programming, softwaredesign
Published: 2023-12-16 15:01:21

Data Access Object design pattern or DAO pattern is a fundamental structural pattern commonly used in Java Applications.

DAO Design Pattern is used to separate low-level data accessing API or operations from high-level business logics.

**Why did we need DAO Design Pattern?**

Before the DAO pattern, applications often directly embedded data access logic within their business logic. This led to tight coupling and several downsides:

- **Difficult Maintenance:** Changes in the database structure or access methods required modifications throughout the application, making maintenance cumbersome.


- **Limited Reusability:** The Data access code was specific to a particular database and couldn't be easily reused in other parts of the application.


- **Reduced Testability:** Testing business logic became more complex due to intertwined data access operations.


The Data Access Object Design Pattern addresses these issues by introducing a dedicated layer for data access.

**Following are the components in DAO Pattern.**

- **DAO Interface:** Defines a standard set of operations that can be performed on a specific data model. This interface remains independent of the underlying database implementation.

- **DAO Implementation:** - This class implements above interface. This class is responsible to get data from a data source which can be database / xml or any other storage mechanism.
  It Provides concrete implementations of the DAO interface using 
  the chosen data access framework (e.g., JDBC)

- **Model Object:** Represents the data model with appropriate properties and methods.

- **Business Logic:** Utilizes the DAO interface to access and manipulate data without any knowledge of the underlying database technology.


**Implementation Example:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cah0zppu7ny0o2ive01r.jpg)




**1. Define the DAO Interface:**
```
interface PersonDao{
	public Person getName(int id) throws SQLException;
	
	public Person getDetails(int Personid) throws SQLException;
}
```


**2. Implement the DAO:**
```
class PersonDaoImplementation implements PersonDao{
	public Person getName(int id) throws SQLException {
		Person p = new Person();
		p.PersonId = id;

	    //fetch data from the database
        String name = DataRequestHandler.getNamefromDataBase(int PersonId) //just an example 
                
		p.name = name;
		return p;
	}

	@Override
	public Person getDetails(int Personid) throws SQLException {
		return null;
	}
}
```


**3. Model Object:**

```
class Person{
	private int PersonId;
	private String name;
	
	public int getPersonId() {
		return PersonId;
	}
	
	public void setPersonId(int personId) {
		PersonId = personId;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
 }
```


**4. Utilize the DAO in Business Logic:**

```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcDaoDemo {
   
	public static void main(String[] args) throws Exception {
		PersonDao dao = new PersonDaoImplementation();
		Person p1 = dao.getName(9);
		System.out.println(p1.getName());
	}
}
```

**Benefits:**

- **Separation of Concerns:** Isolates data access logic, making the business logic cleaner and easier to maintain.

- **Database Independence:** Enables switching databases without affecting the business logic.

- **Encapsulation:** Protects the application from changes in the underlying database implementation.

- **Reusability:** Promotes code reuse for data access operations.


**Conclusion:**

The DAO design pattern plays a crucial role in building clean and maintainable Java applications. Separating data access concerns and promoting abstraction, leads to improved code quality, flexibility, and testability. By understanding and implementing the DAO pattern effectively, developers can build robust and efficient applications that are easier to maintain and adapt to future changes.