Hmmm... but there, the logic for processing the items is held within the items themselves; in fact, I believe the types in question do also use this pattern. What I didn't make clear is that as I want to have different methods for processing the items in different ways, it seems they also need to apply some reasoning based on the type. This does end up happening in some fairly tight loops as well, so if there was a substantial cost I'd like to know.

Quote

I don't think that there is any performance difference. Profile your application before thinking about optimizing performance.

Quite. Must get myself better set up with profiling tools... later. For now, I'm certainly not worried about the cost of instanceof... In fact, I guess I was thinking from more of a design perspective after all. There were comments earlier that people seemed to think instanceof would only end up appearing as the result of some other bad design choice; I'm not so sure, and think maybe the pattern I ended up with is a reasonable one. Comments?

There were comments earlier that people seemed to think instanceof would only end up appearing as the result of some other bad design choice; I'm not so sure, and think maybe the pattern I ended up with is a reasonable one. Comments?

I generally find instanceof to be pretty fragile and a smell that indicates you've got something else wrong somewhere. I only use it when forced to (usually by external APIs), but even that is rare. 'instanceof' is really not too different from random casts - it might work now, but it's very easy to break without warning a few refactorings down the road.

In your particular example, I'd probably just make every Item have a child list and remove the CollectionItem interface totally. If you're worried about the extra overhead you could lazily instantiate the list. Or as already suggested the composite pattern is a good fit here.

Let's say a have a class of type Vehicle which can be a Car or Ship. How would I create the correct panel for the vehicle? I have a CarPanel and a ShipPanel. The best I came up with is to use instanceof. Suggestions how to do it without instanceof?

In your particular example, ... as already suggested the composite pattern is a good fit here.

Probably right. I didn't really think through the properties of that pattern just then (or read the wikipedia page properly). Hardly complicated, I know...

Still, it seems odd that a good solution is to have a high level interface with properties that are only really relevant to a distinct subset of implementations... that said, I don't see it leading to the same problems with refactoring etc that my method, for example, could. Well, tree structures are hardly novel and I suppose established conventions are probably established with good reason.

Ah you want to avoid the dependancy. How about a factory which creates appropriate vehicle/control panel pairs? I assume you've already solved the instantiation problem once somewhere when you decide what specific type of Vehicle to create. If the panel is created much later, then you might want a factory object which remembers what it originally created (and maintains a suitable reference to it).

It feels like you've lost some information by allowing a Car/Ship to decay to a Vehicle and instanceof is the only way to get it back. I guess you've got to either create the right panel when you create the vehicle (before you "lose" the information) or maintain it as extra state (either as a factory object, behind the vehicle interface, or in a higher level class somewhere).

Normally you have this kind of objects in a collection and only want to instanciate the appropriate panel, when you really need to display/edit the object. You even might want to make sure only one panel of a kind exists that can be reused for any object of a type. Or you might want view pooling, if you allow multiple views at a time. Maintaining it as an extra state smells like the "getType()" method already discussed in this thread. I would say, this is a perfect place to use instanceof.

I agree with princec here. Java developers should learn at least one thing from .NET developers: to think "You're not gonna need it!" when you are thinking "what if, what if, what if". It saves a lot of design and development time and head aches. With today's tools it is so much easier to refactor a piece of code should you need to expand something you have hardcoded into something more abstract.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org