How can I maintain immutability of objects when they contain lists that must be made available to clients of the class?
For example, I have a class that has an ArrayList of documents, and it has a method:
<code>public List getDocuments() </code>
Of course, someone can obtain the reference to the list, get an Iterator, and begin removing anything they want. It seems to me as though this violates the public/private interface.
Is there an easy way to prevent this sort of tampering? I could extend ArrayList to disallow add/remove and implement my own Iterator class that refuses the remove option, but this seems like overkill. Is there a better way to do it? Are there any
Java books that discuss this?
Thanks,
Micah
[ July 22, 2002: Message edited by: Micah Wedemeyer ]