문제

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