0
0
JavaConceptBeginner · 3 min read

DAO Pattern in Java: What It Is and How It Works

The DAO (Data Access Object) pattern in Java is a design approach that separates the data access logic from business logic by using an interface and its implementation to handle database operations. It helps keep code organized, easier to maintain, and allows changing the data source without affecting other parts of the application.
⚙️

How It Works

Imagine you have a store where you keep all your books, but you don't want everyone to go inside and mess with the shelves. Instead, you have a librarian who knows exactly how to find, add, or remove books. In Java, the DAO pattern acts like that librarian. It provides a simple way for the rest of your program to get or save data without knowing the details of how the data is stored.

The DAO pattern uses an interface to define methods like save, update, delete, and find. Then, a class implements this interface to connect to the database or any data source. This way, if you decide to change your database or storage method later, you only need to update the DAO implementation, not the whole program.

💻

Example

This example shows a simple DAO for managing User objects with methods to add and find users.

java
import java.util.*;

// User model class
class User {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

// DAO interface
interface UserDao {
    void addUser(User user);
    User getUser(int id);
}

// DAO implementation using a simple list as storage
class UserDaoImpl implements UserDao {
    private List<User> users = new ArrayList<>();

    @Override
    public void addUser(User user) {
        users.add(user);
    }

    @Override
    public User getUser(int id) {
        for (User user : users) {
            if (user.getId() == id) {
                return user;
            }
        }
        return null;
    }
}

// Main class to test DAO
public class Main {
    public static void main(String[] args) {
        UserDao userDao = new UserDaoImpl();
        userDao.addUser(new User(1, "Alice"));
        userDao.addUser(new User(2, "Bob"));

        User user = userDao.getUser(1);
        if (user != null) {
            System.out.println("User found: " + user.getName());
        } else {
            System.out.println("User not found");
        }
    }
}
Output
User found: Alice
🎯

When to Use

Use the DAO pattern when your application needs to interact with a database or any data source and you want to keep your code clean and easy to manage. It is especially helpful in large projects where many parts of the program need to access data but should not know how the data is stored.

For example, if you build a web app that stores user information, using DAO lets you change from a file system to a database or from one database type to another without rewriting your business logic. It also makes testing easier because you can replace the real data access with fake data during tests.

Key Points

  • The DAO pattern separates data access from business logic.
  • It uses interfaces to define data operations.
  • Implementations handle the actual database or storage details.
  • It improves code maintainability and flexibility.
  • It simplifies testing by allowing mock implementations.

Key Takeaways

DAO pattern separates how data is accessed from how it is used in the program.
It uses interfaces and implementations to organize database operations cleanly.
DAO makes it easy to switch data sources without changing business logic.
It improves code maintenance and testing by isolating data access.
Use DAO in projects that require clear separation between data and logic.