java - Why are iterators private? -
iterators nested classes exported clients. why declared private instead of public ? eg:
private abstract class hashiterator<e> implements iterator<e> {
private final class entryiterator extends hashiterator<map.entry<k,v>> { public map.entry<k,v> next() { return nextentry(); } }
in general case, don't have private; i.e. if designing data structure, there nothing stopping declaring iterator class public
.
however, private iterators "good design" if data structure intended string abstraction; i.e. 1 internal representation hidden client code.
one reason making iterator class private prevents undesirable coupling; i.e. stops external class depending on actual iterator code in way make future code changes harder.
another reason in cases extensible public iterator class couldn't instantiated anyway. or @ least, not without relaxing abstraction boundary of data structure.
another way @ making iterator class public
not achieve anything. caller typically doesn't need use apart methods in iterator
api. , if does, solution extend iterator api (interface) provide additional methods.
Comments
Post a Comment