I find there is a tension between the principles of "tell don't ask" and model view seperation. For example, I always end up with code like this :
Not ideal, switching on the objects type.
However to maintain model/view seperation, I cannot ask the object for it's image.
I could also create a map of class to image, but that is just another way of writing the switch statement, and it doesn't handle subclasses so well
I've tried using a double dispatch pattern, and while this does get rid of the switch, it makes the code much more confusing.
So in the end, I just live with the switch statement. Are there any other patterns/approaches I could use here?
You could simulate a double dispatch approach if you really want to, but it makes the code much more complex and it doesn't do much to improve maintainability either:
So exactly how far are you willing to take this to avoid those instanceof checks? ;)
Edit: Yeah, so I sort of skimmed over the part of your OP where it says you already had a look at the DD approach, and shot it down for the same reason I mentioned. I'll stop wasting everybody's time now...
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Joined: Dec 12, 2002
Exactly. So just live with conditional is probably the best bet. Ta.
The only problem I see with the conditionals is that this way the method is tightly coupled to all possibly existing types. At some point, you might want to separate some of those types into different, potentially optional, moduls. Then it really starts to make sense to use some kind of registry, like a HashMap - one, that doesn't get filled from a central place, but that different moduls/plugins contribute entries to.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus