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

本文是小编为大家收集整理的关于什么是DAO工厂模式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

  1. 我想知道它的重要性.
  2. 其用法

我已经检查了这个链接但是我很难理解.

任何人都可以在示例的帮助下解释吗?

编辑:这是我理解的DAO模式的一个示例:

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

实施:

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

工厂:

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

客户端代码:

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

这个dao模式正确吗?

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

推荐答案

您可能不了解代码的工作原理?看起来很好.

仅供参考:

  • 如果您考虑将其命名为UserDaomySqlimpl,而将其命名为UserDaomySqlimpl,则可以更好地理解定义为UserDaoimpl的内容,而另一个新的则为UserDaomssqlimpl,则对于您可能需要的每个数据库访问,依此类推.

  • 在每一个中,您都应该处理连接并添加其他内容,例如该特定数据库服务器配置的私有功能,它可能需要,并且不需要在接口(userdao)中声明,但最少必须始终始终实现界面中定义的所有方法,然后在工厂(用户达法)条件中,您可以拥有类似的东西:

`

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");

您可以拥有一个属性文件,以便能够在Daos之间进行动态切换,因为您可以简单地从属性文件中检索该字符串:

UserDAO userDAO=UserDAOFactory.getUserDAO(myStringFromPropertiesFile);

根据属性文件中的定义,将包含" mysql"或" mssql".

希望这会有所帮助!

其他推荐答案

DAO代表"数据访问对象".这是一个基于接口的类,可以使用特定对象的关系数据库来处理所有CRUD操作.这是一个使用仿制药的示例:

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

将工厂视为"虚拟构造函数":其创建方法返回接口类型,但是您可以要求它根据需要创建任何数量的不同实现.

本文地址:https://www.itbaoku.cn/post/627738.html

问题描述

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 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.