• Post Reply Bookmark Topic Watch Topic
  • New Topic

OOD Q: using objs that implement multiple interfaces  RSS feed

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an app I'm writing and trying to user more interfaces. so say I have a code like:




the point of this is so client code can deal with the interface and not the implementation, rather deal with interfaces and factories; but I'm finding that I have to do a lot of casting, so in a case where I have a Book b, I then need to cast to Commentable to get the comments.

What's the best way to minimize that? Create a facade interface that implements them all? By using the implementing class, I'm defeating the purpose of creating the interface structure.
thanks!
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a Book object is always Commentable, then you could certainly submerge the Commentable methods into the Book interface. But if there's the possibility of non-Commentable Book objects, or Commentable objects which aren't Books, then you shouldn't do that. At any rate you shouldn't make the decision solely on whether you can reduce the amount of casting in your code. Sure, casting is a hint that your data design might not be right, so you should take that into account, but you need to let the data design stand on its own two feet.
 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, glad to know that I'm not the only one who struggles with that. The other option I was thinking of. Not sure if I'm repeating what you just said, but the Book interface could also extend the Commentable interface. And the DataAccessor should be in another package altogether, maybe decorating the Book.

 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edmund Castermund wrote:Not sure if I'm repeating what you just said, but the Book interface could also extend the Commentable interface.


Yup, if a Book is always Commentable then you could certainly do that.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edmund Castermund wrote:...And the DataAccessor should be in another package altogether, maybe decorating the Book.

Doesn't sound quite right as a Decorator, because a DataAccessor is likely to need access to pretty much all the internals of a Book. A nested class perhaps, viz:although you might need to tweak it a bit.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!