Re: How to give selective access to the methods in a class?
toton wrote:
Eric Sosman wrote:
[...]
Document can contain as many Headers and Segments as you
choose, but cannot modify them (it does not have access to
their setters). In this way Document resembles ArrayList or
HashSet: its job is to hold things, evict things, dole out
references to things, and tell you whether certain things are
or aren't there, but not to make changes to those things.
Can Document hold reference to an inner class?
Yes. Inner classes are classes.
No, Document is not an ArrayList. It holds exactly one Header, and an
ArrayList of Segment's , and can return Document or an element from
Segment ArrayList ( dont allow to add an element to the ArrayList or
remove, just returns the Iterator) .
I didn't mean that Document *is* an ArrayList or HashSet,
I meant that it is *similar* to those. The similarity I wanted
to point out is that Document's role is to hold on to a bunch of
references to Segments and Headers and Whatnots, but not to make
changes to those things. The methods of Document cannot call the
setters of Segment, because they are private. Document can change
the contents of its ArrayList of Segment references, adding new
ones and evicting old ones, but it cannot make changes to the
Segment instances themselves.
By the way, Iterator has a remove() method. If you want to
make sure nobody except Document can change the "membership" of
the ArrayList, be sure to derive the Iterator from an unmodifiable
view of the ArrayList and not from the ArrayList itself. See
Collections.unmodifiableList().
--
Eric Sosman
esosman@acm-dot-org.invalid