Question

I use DAO to handle data, but the DAO is just an interface, I have several concrete DAO objects for different objects. So I have to control what DAO I use. The following are code that a factory object create a DAO object:

Factory factory = DAOFactory.getInstance();
ParkingLotDAO dao = (ParkingLotDAO)factory.create(daoType);

As can be seem that I use String(daoType is a String) to control which DAO the factory should create, problem is, String is hard to maintain and easy to go wrong. Is there better way to do it?

Was it helpful?

Solution

You may use:
Factory method (creational methods returning an implementation of an abstract/interface type)

ParkingLotDAO dao = (ParkingLotDAO)factory.create(IParkingLotDAO);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top