DEV Community

Ghazanfar Mumtaz
Ghazanfar Mumtaz

Posted on

DAO Design Pattern

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

1. Define the DAO Interface:

interface PersonDao{
    public Person getName(int id) throws SQLException;

    public Person getDetails(int Personid) throws SQLException;
}
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
    }

 }
Enter fullscreen mode Exit fullscreen mode

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());
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
01dri profile image
Diego Henrique

Interesting this pattern.

I normally use the DTO pattern in my applications