Invariable collection?

I use the readonly collection to prevent users from modifying my cached data, but the elements themselves are, of course, mutable. Is there a way to prevent this behavior?

Michelle

+4
source share
3 answers

Either make the cached items immutable, or skip copies of objects.

+6
source

you can set the private constructor, so only internal elements can instantiate.

Alternatively, you can use the constructor to place read-only values ​​....

0
source

One way is to have both an immutable and a modified version of each object of the business model. I wrote an article about it here: http://rickyhelgesson.wordpress.com/2012/07/17/mutable-or-immutable-in-a-parallel-world/

0
source

Source: https://habr.com/ru/post/1384692/


All Articles