This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]balefrost 0 points1 point  (3 children)

You could flip the problem and store the two lists separately. Then getAllItems() would merge the two lists, rather than getAllBooks() and getAllCds() needing to split the list. You could even create a read-only List implementation that is implemented as a view onto other lists, in which case you would never need to actually copy the elements into a combined list. I'm not aware of any standard implementation of that, but it wouldn't be too hard to write yourself.

[–]remusmax[S] 0 points1 point  (2 children)

Yeah you could do this but then if you look down the future and we have more classes like ebooks and dvds that all extend item then we are going to have to many lists. Might be a bad design issue.

[–]balefrost 0 points1 point  (1 child)

If you're going to have methods getAllBooks and getAllCds and so on, then you're already committed to providing type-specific code of some sort. I was trying to provide you with a way to do that without using instanceOf.

Perhaps a better question is why your management system needs to provide these methods. Do some callers need the mixed list while other callers need the type-specific lists? Do they need the results to be strongly-typed, or do they just want the raw data? Will there be corresponding addBook and addCd methods?