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?

Était-ce utile?

La solution

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

ParkingLotDAO dao = (ParkingLotDAO)factory.create(IParkingLotDAO);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top