• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design question: interface of TreeNode, BinaryNode and LeafNode

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, people:

This is a question about design, not the language details.
I would like to listen to your guys' opinions on the following design ideas, which might be simple in this case, but the same principles can be extended into more complex systems.

Background:
A set of data structures is wanted to represent a tree, any of whose node can have an arbitrary number of children.
It is required that TreeNode (the ordinary ones), LeafNode and BinaryNode (having two children) are implemented in three classes.

What I think:
I'm going to write an AbstractTreeNode with common things among the three type of nodes and inherit from it to implement all the three types of nodes.
I'm also going to have an interface for the AbstractTreeNode class.

My question is, for TreeNode, methods like getNumberOfChildren() make perfect sense, but such methods don't mean anything to LeafNode(although I can have dummy implementations for them, like returning 0). In terms of good Object Oriented design, should the three types of nodes share one interface? My concern is, if they share the same interface, then from the point of view of an outsider, a LeafNode would have a method like getNumberOfChildren(), which sounds bad.

If you have any suggestions about a better design, please let me know. Thank you.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion the idea of having a separate class for LeafNode is questionable. It implies that once you have a leaf node in your tree, you can never modify that node so that it has children. This might actually be the case in some environments but it doesn't work for a general-purpose implementation. In a general-purpose implementation you would have just a Node class with an isLeaf() method to tell you whether it happened to be a leaf node at the moment.

As for BinaryNode, it's even worse because it violates the Zero-One-Infinity rule.

So, if these are actual real-life requirements, it would be necessary to inquire into the reasons why those classes existed before you could produce a reasonable design. That's because given those questionable-looking requirements, there may be other requirements which haven't appeared yet.
 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:In my opinion the idea of having a separate class for LeafNode is questionable. .

As for BinaryNode, it's even worse because it violates the Zero-One-Infinity rule.




Thanks for the reply. I know what you mean and I strongly agree with you.
However, the requirement comes from a programming exercise, which claims that the whole point is to give exercise takers some ideas on how to separate interface from implementation and use of abstract class for elimination of duplicate code.

The solution has one interface for AbstractTreeNode and every node type inherits the AbstractTreeNode. So when using the three types of nodes, people can just do: and stuff like that. It will also enable other developers to use polymorphism when accepting my_node into functions written by them.

I've got no problem understanding the idea, but it just sounds bad to me that a class has some meaningless methods defined in its interface. However, I do hear that such things happen in Swing's implementation. If I recall correctly, it's JComponent or something (Is it true? I'll be very happy if someone can verify this). So I start to get confused ... mainly about that a class has meaningless methods in its interface. Is it a bad design that should always be avoided? or something that is still acceptable in industry/academia whatever?

Thanks.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It happens all the time. Look at the collections like List and Set, for example. In general you'll want to be able to add entries to a List, so therefore List has an add(E) method. But if you want a subclass of List which is immutable, then what? Now your add(E) method has to throw a runtime exception if it's called.

So what could we do differently? Could the List interface inherit from an ImmutableList interface which has fewer methods than List does? Not really, because then your implementation of the ImmutableList interface wouldn't be a List. And that isn't very useful. So you're stuck either way.

In other words, yeah, sometimes your subclasses have to have fewer capabilities than the classes they extend. The people in academia no doubt have a name for this phenomenon, and the people in industry look at it and say "Oh well" and carry on.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Castor Tack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Paul:
Thanks a lot. Your answer is helpful.

To Campbell:
Thanks, this looks like a nice place for me.
reply
    Bookmark Topic Watch Topic
  • New Topic