HVDSV
03-12-2016, 07:34
Sto cercando di capire come funziona il pattern DAO però trovo implementazioni discordanti.
In particolare il DAOFactory deve esser fatto in questo modo:
// Avere una sola implementazione di CarDAO,
// ad esempio CarDAOImpl, e a seconda se è
// MySQL o SQLServer usare query diverse
public abstract class DAOFactory
{
public static DAOFactory mySqlDaoFactory = null;
public static DAOFactory sqlServerDaoFactory = null;
public static DAOFactory getDAOFactory(DatabaseType dbType, String host, String port, String dbName, String user, String passw, int timeoutMilliseconds) throws DBException, ClassNotFoundException
{
if (DatabaseType.MySQL == dbType)
{
if (null == mySqlDaoFactory)
{
mySqlDaoFactory = new MySQLDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
return mySqlDaoFactory;
}
else if (DatabaseType.SQLServer == dbType)
{
if (null == sqlServerDaoFactory)
{
sqlServerDaoFactory = new SqlServerDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
return sqlServerDaoFactory;
}
else
{
throw new DBException("Unsupported Db type");
}
}
public CallDAO getCarDAO()
{
return new CarDAOImpl(this);
}
oppure in questo:
// Implementazione di getCarDAO sia in MySQLDAOFactory che in SqlServerDAOFactory
// Avere poi una doppia implementazione di CarDAO, ad esempio MySQLCallDAOImpl e SqlServerCallDAOImpl
public abstract class DAOFactory
{
public static DAOFactory daoFactory = null;
public abstract CarDAO getCarDAO();
public static DAOFactory getDAOFactory(DatabaseType dbType, String host, String port, String dbName, String user, String passw, int timeoutMilliseconds) throws DBException, ClassNotFoundException
{
if (null == daoFactory)
{
if (DatabaseType.MySQL == dbType)
{
daoFactory = new MySQLDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
else if (DatabaseType.SQLServer == dbType)
{
daoFactory = new SqlServerDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
else
{
throw new DBException("Unsupported Db type");
}
}
return daoFactory;
}
}
Io credo la seconda, ma mi date certezze? :sofico:
In particolare il DAOFactory deve esser fatto in questo modo:
// Avere una sola implementazione di CarDAO,
// ad esempio CarDAOImpl, e a seconda se è
// MySQL o SQLServer usare query diverse
public abstract class DAOFactory
{
public static DAOFactory mySqlDaoFactory = null;
public static DAOFactory sqlServerDaoFactory = null;
public static DAOFactory getDAOFactory(DatabaseType dbType, String host, String port, String dbName, String user, String passw, int timeoutMilliseconds) throws DBException, ClassNotFoundException
{
if (DatabaseType.MySQL == dbType)
{
if (null == mySqlDaoFactory)
{
mySqlDaoFactory = new MySQLDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
return mySqlDaoFactory;
}
else if (DatabaseType.SQLServer == dbType)
{
if (null == sqlServerDaoFactory)
{
sqlServerDaoFactory = new SqlServerDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
return sqlServerDaoFactory;
}
else
{
throw new DBException("Unsupported Db type");
}
}
public CallDAO getCarDAO()
{
return new CarDAOImpl(this);
}
oppure in questo:
// Implementazione di getCarDAO sia in MySQLDAOFactory che in SqlServerDAOFactory
// Avere poi una doppia implementazione di CarDAO, ad esempio MySQLCallDAOImpl e SqlServerCallDAOImpl
public abstract class DAOFactory
{
public static DAOFactory daoFactory = null;
public abstract CarDAO getCarDAO();
public static DAOFactory getDAOFactory(DatabaseType dbType, String host, String port, String dbName, String user, String passw, int timeoutMilliseconds) throws DBException, ClassNotFoundException
{
if (null == daoFactory)
{
if (DatabaseType.MySQL == dbType)
{
daoFactory = new MySQLDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
else if (DatabaseType.SQLServer == dbType)
{
daoFactory = new SqlServerDAOFactory(host, port, dbName, user, passw, timeoutMilliseconds);
}
else
{
throw new DBException("Unsupported Db type");
}
}
return daoFactory;
}
}
Io credo la seconda, ma mi date certezze? :sofico: