Although you could have immutable classes, just implementing clone to return references to yourself, I really don't see much value when using clone for things that may or may not be mutable, while there is no way to create mutable deals with immutable things and vice versa .
I would think that it would be better for your Matrix base class to include the IsImmutable and IsWritable , as well as the AsImmutable , AsMutable and AsNewMutable ; it should also include methods for reading and writing the matrix (although calling the "write" method on an unwritable matrix should throw an exception).
Define the static methods CreateImmutableMatrix and CreateMutableMatrix , which, under the Matrix condition, will create a new immutable or mutable matrix that is pre-initialized with the appropriate data.
Variable classes must implement AsImmutable to go to CreateImmutableMatrix , AsMutable to return itself, and AsNewMutable to go to CreateMutableMatrix .
Immutable classes must implement AsImmutable to return themselves, AsMutable to call AsNewMutable and AsNewMutable to go to CreateMutableMatrix .
Read-only shells must implement AsImmutable to call AsImmutable for wrapped objects, and AsMutable and AsNewMutable to call AsNewMutable for wrapped objects.
An object that receives a matrix, which it may or may not copy or mutate, can simply store it in a field (for example, Foo ). If he needs to mutate the matrix, it can replace Foo with Foo.AsMutable() . If the object containing the matrix needs to be copied, this field should be replaced in the copy using Foo.AsImmutable() or Foo.AsNewMutable() , depending on whether the field in the copy is likely to be mutated.
source share