DAOFactory

public abstract class DAOFactory extends Component

The DAO pattern can be made highly flexible by adopting the Abstract Factory and the Factory Method Patterns.

The Factory Method pattern can be used to produce a number of DAOs needed by the application when the underlying storage is not subject to change from one implementation to another. When it is subject to change, the Abstract Factory pattern can in turn build on and use the Factory Method Implementation. In this case, it provides an abstract DAO factory object that can construct various types of concrete DAO factories, each factory supporting a different type of persistent storage implementation. Once you obtain the concrete DAO factory for a specific implementation, you use it to produce DAOs supported and implemented in that implementation.

Author:Hugo Y. K. Lam

Constructors

DAOFactory

protected DAOFactory()

Creates a new instance of DAOFactory.

Methods

createDAO

public DAO createDAO(Class inf)

Creates a new DAO.

Parameters:
  • inf – the DAO interface class.
Throws:
  • DAOException – if there is any problem in looking up the DAO with the interface specified.
Returns:

the DAO retrieved by the interface specified.

createDAO

public DAO createDAO(String daoname)

Creates a new DAO.

Parameters:
  • daoname – the DAO name.
Throws:
  • DAOException – if there is any problem in looking up the DAO with the name specified.
Returns:

the DAO retrieved by the name specified.

See also: .initDAO(DAO)

createDAOFactory

public static DAOFactory createDAOFactory(String name, String provider, Properties params, ClassLoader loader)

Creates a DAO Factory.

Parameters:
  • name – the DAO Factory name.
  • provider – the provider of the DAO Factory.
  • params – the parameters for creating the DAO Factory.
  • loader – the class loader for loading the factory class.
Throws:
  • DAOException – if there is any error when creating the DAO Factory using the specified parameters.
Returns:

the DAO Factory created by the parameters specified.

createTransaction

public Transaction createTransaction()

Creates a new transaction.

Throws:
Returns:

a new transaction.

getParameter

protected String getParameter(String key)

Gets a parameter from the parameters of this DAOFactory.

Parameters:
  • key – the parameter key.
Throws:
  • DAOException – if there is no parameter matching the specified key.
Returns:

the parameter value associated with the specified key.

getParameter

protected String getParameter(String key, String def)

Gets a parameter from the parameters of this DAOFactory.

Parameters:
  • key – the parameter key.
  • def – the default value.
Returns:

the parameter value associated with the specified key. The default value will be returned if there is no parameter matching the specified key.

init

protected void init()

Initializes the DAO Factory.

Throws:
  • DAOException – when there is any error in the initialization.

See also: .initFactory()

initDAO

protected abstract void initDAO(DAO dao)

Invoked by the createDAO() method for initializing the given DAO.

Parameters:
  • dao – the DAO.
Throws:

See also: .createDAO(Class), .createDAO(String)

initFactory

protected abstract void initFactory()

Invoked by the init() method for initializing the implementing factory.

Throws:

See also: .init()