質問

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