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?

有帮助吗?

解决方案

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

ParkingLotDAO dao = (ParkingLotDAO)factory.create(IParkingLotDAO);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top