In java i am writing some DTO objects, which all inherit from AllocationDTO
.
A list of these objects are then passed into a DAO object for saving to the database.
Depending on which subtype of AllocationDTO
is being saved the saving logic changes (e.g. which table in the database to save to etc.)
I find myself using code as such:
for (AllocationDTO x : listOfAllocationDtos) {
if (x instanceof ManagerAllocationDTO) {
Manager m = (x(ManagerAllocationDTO)).getManager();
// save manager etc to managerallocs
} else if (x.getId() == AllocationDTO.TYPE_SPECIAL1) {
// save to specialAlloc1 table
} else if (x.getId() == AllocationDTO.TYPE_SPECIAL2) {
// save to specialAlloc2 table
}
}
The ManagerAllocationDTO
has an extra field relating the allocation to a manager, but for the specialalloc1/2 cases I have not made a subtype because the only difference in the data is the table it is saved to.
My question is a bit of a soft design question - is this the best way to go about doing this?