• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tell don't ask and model view seperation

 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conditional code is fine. What make you think that it is a problem?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. So just live with conditional is probably the best bet. Ta.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not too much fun in Java, but I've often used decorator-like things when stuff like this pops up.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic