piglet
Posts: 63
Nickname: piglet
Registered: Dec, 2005
|
|
Collection is generic
|
Posted: Dec 5, 2005 11:35 AM
|
|
I am surprised that Bruce, of all people, would question the Collection interface. The point of having a common interface is simply to allow the implementation of algorithms in the most general way possible - aka generic programming. It is the Java way of doing generic stuff. For example, when you look at the Collections class, you'll find several useful methods that make sense for any Collection. addAll(), disjoint(), frequency(), min() and max() are applicable to "anything holding objects. The Java way of saying "this is an aggregate of objects" is to have it implement Collection. List and Set have different functions, but nevertheless it may make sense to check whether a given List has an element in commen with a given Set, or to put the elements of a List on a Queue. Without the Collection interface, you'd have to implement non-generic methods for any situation that might possibly arise in order to do stuff like that.
It is true, you could often accomplish the same with iterators, but not always (as has been pointed out before), so using the Collection interface seems to be the most generic solution. I also have the feeling that the idiom is not very appropriate, at least not for Java. Even if disjoint() could be implemented to take Iterators (which it cannot), it wouldn't really make sense to speak of "disjoint iterators" rather than disjoint collections. Collections have max and min elements, size, an iterator, array representations, can be empty or not. This is really behavior that all Collections have in common and it justifies the Collection interface. Some design details of the API are of course questionable but in principle it works fine for me.
"I would argue that what they appear to have in common is structural rather than functional: all three involve holding objects." Yes, but that's the point. That's why we need this interface, in order to express structure. It's marvellous that we can do this! Same for Iterator, it expresses structure and not function.
|
|