38

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?

coder25
  • 2,363
  • 12
  • 57
  • 104
  • 5
    Your `MammalsFactory` creates `SQL`'s and `Oracle`'s ? Interesting ;) – Felix Kling Jun 19 '11 at 09:13
  • Have you already checked the example code in section "Using Abstract Factory Pattern" on http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html ? – joschi Jun 19 '11 at 09:15
  • yes i have but i am unable to understand... – coder25 Jun 19 '11 at 09:16
  • Assuming you have some implementation of `Database`, `sql` and `oracle`, you've already successfully implemented the abstract factory method pattern. You just need to figure out how to implement `sql` and `oracle` as DAOs. – Merlyn Morgan-Graham Jun 19 '11 at 09:40
  • Your new implementation looks about right. As for connections, the DAO itself probably shouldn't control it. Either the factory should, or the user should. That way you can share connections between DAOs. – Merlyn Morgan-Graham Jun 20 '11 at 06:44

2 Answers2

24

DAO stands for "Data Access Object". It's an interface-based class that handles all your CRUD operations with a relational database for a particular object. Here's an example that uses generics:

package persistence;

public interface GenericDao<K extends Serializable, T> 
{
    public T find(K id);
    public List<T> find();
    public K save(T value);
    public void update(T value);
    public void delete(T value);
}

Think of a factory as a "virtual constructor": its creation method returns an interface type, but you can ask it to create any number of different implementations as needed.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 2
    @prema - that's OK. You don't have to use generics. – Stephen C Jun 19 '11 at 09:20
  • 18
    @prema: This is not a *give me code* website. If you still don't understand it, ask a specific question. – Felix Kling Jun 19 '11 at 09:26
  • 2
    -1. You're mixing the singleton pattern with the factory pattern, and not implementing the abstract factory pattern. I'm not sure if this is what he wanted or not, but you're not explaining any of the differences. This probably will cause him more confusion. – Merlyn Morgan-Graham Jun 19 '11 at 09:29
  • @duffymo: The DAO explanation is perfect, and I can't improve on it. The OP's factory implementation (more or less) correctly implements the abstract factory pattern, so maybe you should just remove that portion. Sorry, I'll remove the downvote. I just go a little nuts when people use singletons ;) – Merlyn Morgan-Graham Jun 19 '11 at 09:36
  • I don't care for singletons either. I'll remove it and make both of us feel better. – duffymo Jun 19 '11 at 09:44
  • I have understood the interface part but the second code is creating confusion..... – coder25 Jun 19 '11 at 09:55
  • i have edited the code to implement dao.Is the method correct – coder25 Jun 19 '11 at 10:10
  • @prerna: Your added code seems to match duffy's answer, so yeah it looks about right. – Merlyn Morgan-Graham Jun 20 '11 at 06:44
22

Probably what you don't understand is how the code works? It seems fine.

Just FYI:

  • What is defined as UserDAOImpl can be better understood if you consider naming it as UserDAOMySQLImpl and another new one as UserDAOMSSQLImpl, and so on for each database access you may need.

  • In each of those you should handle the connections and add other things like private functions for that specific database server configuration it may need and not forcibly needed to be declared in the interface (UserDAO) but as minimum you must always implement all the methods defined in the interface, then in the Factory (UserDAOFactory) conditions you could have something like this:

`

public class UserDAOFactory{

    public static UserDAO getUserDAO(String type){ 
        if (type.equalsIgnoreCase("mysql")){
            return new UserDAOMySQLImpl();
        }else{
            return new UserDAOMSSQLImpl();
        }
    }
}

A little clearer?

Then, in the client side instead of a hardcoded line like:

UserDAO userDAO=UserDAOFactory.getUserDAO("jdbc");

You could have a properties file to be able to switch between DAOs dynamically, having retrieved that string from the properties file you can simply do:

UserDAO userDAO=UserDAOFactory.getUserDAO(myStringFromPropertiesFile);

myStringFromPropertiesFile would contain "mysql" or "mssql" according to the definition in your properties file.

Hope this helps!

Jason Krs
  • 704
  • 2
  • 9
  • 30