Java Vibes..

June 22, 2009

Abstract Factory Pattern

Filed under: architecture & design — harjitdelhi @ 10:13 am
Tags: , , , ,

I had implemented an Abstract Factory in one of my projects. Posting the details of the implementation here.

The requirement is that for CDR database, there are 2 datasources, one for HongKong and one for US. And for ProdProfile database, there is only datasource for both locations. The location is not known at the compile time so we can’t inject datasource in the factory at compile time. Also passing the location in the getDao() method looks ugly since location is not required for all type of DAOs. And even ProductProfile datasource may become location specific in future.

I have tried to make one factory for each Location. The factory will instantiate different DAOs, whether the DAO is injected in the factory is location specific or not is controlled by Spring config.

AbstractDaoFactory:

CdrDao getCdrDao()
ProdProfileDao getProdProfileDao()
USDaoFactory extends AbstractDaoFactory
CdrDao getCdrDao()
ProdProfileDao getProdProfileDao()
HKDaoFactory extends AbstractDaoFactory
CdrDao getCdrDao()
ProdProfileDao getProdProfileDao()
CdrDao
setDS()
loadProductList()
CdrDaoImpl: 2 spring beans for this Impl, but different DS injected in these: USCdrDaoImpl, HKCdrDaoImpl
ChecklistDao
setDS()
loadChecklist()
ChecklistDaoImpl: just 1 spring bean for this Impl, so both factories will return the same instance
Class AbstractDaoFactory:
+ CdrDao getCdrDao()
+ ProdProfuleDao getProdProfileDao()
Class USDaoFactory extends AbstractDaoFactory
+ CdrDao getCdrDao()
+ ProdProfileDao getProdProfileDao()
Class HKDaoFactory extends AbstractDaoFactory
+ CdrDao getCdrDao()
+ ProdProfileDao getProdProfileDao()
Interface CdrDao
+ setDs(DataSource ds)
+ loadLegCodes()
Interface ProdProfileDao
+ setDs(DataSource ds)
+ loadAllSeries()
Class CdrDaoImpl: created 2 spring beans for this Impl, but different DS injected in these: USCdrDaoImpl, HKCdrDaoImpl
Class ProdProfileDaoImpl: created just 1 spring bean for this Impl, so both factories will return the same instance

Blog at WordPress.com.