What is DAO factory pattern?

Viewed 58017

I am aware of factory and abstract factory methods, but I want to create a DAO factory pattern in Java.

  1. I want to know its importance.
  2. Its usage

I have checked this link but it is difficult for me to understand.

Can anyone explain it with the help of an example?

Edit: Here is an example of DAO pattern as I understood it:

public interface UserDAO {
    public void insert(User user);
    public void update(User user);
    public void delete(int userId);
}

Implementation:

public class UserDAOImpl implements UserDAO {
    @Override
    public void delete(int userId) {
        // delete user from user table
    }

    @Override
    public User[] findAll() {
        // get a list of all users from user table
        return null;
    }

    @Override
    public User findByKey(int userId) {
        // get a user information if we supply unique userid
        return null;
    }

    @Override
    public void insert(User user) {
        // insert user into user table
    }

    @Override
    public void update(User user) {
        // update user information in user table
    }
}

Factory:

public class UserDAOFactory {
    public static UserDAO getUserDAO(String type) { 
        if (type.equalsIgnoreCase("jdbc")) {
            return new UserDAOImpl();
        } else {
            return new UserDAOImpl();
        }
    }
}

Client side code:

User user=new User();
user.setName("Jinoy P George");
user.setDesignation("Programmer");
user.setAge(35);
//get a reference to UserDAO object
UserDAO userDAO=UserDAOFactory.getUserDAO("jdbc");
//call insert method by passing user object
userDAO.insert(user);

Is this dao pattern correct?

Where should I open connection and close it?

2 Answers
Related