什么是DAO工厂模式? [英] What is DAO factory pattern?

查看:166
本文介绍了什么是DAO工厂模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道工厂和抽象的工厂方法,但是我想在Java中创建一个DAO工厂模式。


  1. 我想知道它的重要性。

  2. 它的用法

我已经检查了此链接,但我很难理解。



/ p>

编辑:下面是我所理解的DAO模式的一个例子:

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

实施:

  public class UserDAOImpl实现UserDAO {
@Override
public void delete(int userId){
//从用户表中删除用户
}

@Override
public User [] findAll(){
//从用户表获取所有用户的列表
return null;
}

@Override
public用户findByKey(int userId){
//如果我们提供唯一的用户ID
返回null;
}

@Override
public void insert(User user){
//将用户插入用户表
}

@Override
public void update(User user){
//更新用户表
中的用户信息
}

工厂:

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

客户端代码: p>

  User user = new User(); 
user.setName(Jinoy P George);
user.setDesignation(Programmer);
user.setAge(35);
//获取UserDAO对象的引用
UserDAO userDAO = UserDAOFactory.getUserDAO(jdbc);
//通过传递用户对象调用insert方法
userDAO.insert(user);

这个dao模式是否正确?



我应该在哪里打开连接并关闭它?

解决方案

可能你不明白代码是如何工作的?



只是FYI:




  • 什么是定义如果您将UserDAOImpl命名为UserDAOMySQLImpl并将其另一个命名为UserDAOMSSQLImpl,那么可以更好地理解UserDAOImpl,对于每个可能需要的数据库访问等等。


  • 你应该处理这些连接的每一个,并添加其他的东西,比如私有功能,这个特定的数据库服务器配置可能需要,而不是强制需要在接口(UserDAO)中声明,但是最起码你必须始终实现在界面,然后在工厂(UserDAOFactory)条件下,您可以这样做:




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



有点更清楚?



然后,在客户端而不是硬编码行,如:

  UserDAO userDAO = UserDAOFactory.getUserDAO( JDBC); 

您可以将属性文件动态地切换到DAO之间,从属性文件你可以简单地做:

  UserDAO userDAO = UserDAOFactory.getUserDAO(myStringFromPropertiesFile); 

根据属性文件中的定义,myStringFromPropertiesFile将包含mysql或mssql p>

希望这有帮助!


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?

解决方案

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!

这篇关于什么是DAO工厂模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆