my dog learned polymorphism
The moose likes OO, Patterns, UML and Refactoring and the fly likes Tell don't ask and model view seperation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Tell don Watch "Tell don New topic

Tell don't ask and model view seperation

Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580

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?

thanks, Don.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Conditional code is fine. What make you think that it is a problem?

SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

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.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Exactly. So just live with conditional is probably the best bet. Ta.
David Newton

Joined: Sep 29, 2008
Posts: 12617

It's not too much fun in Java, but I've often used decorator-like things when stuff like this pops up.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
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
I agree. Here's the link:
subject: Tell don't ask and model view seperation
It's not a secret anymore!