Feb 21, 2010

Shallow cloning Vs Deep cloning


The default behavior of an objects clone() method automatically yields a shallow copy. So to achieve a deep copy the classes mush be edited or adusted.

Shallow copy:
            If a shallow copy is performed on obj-1 as shown in fig-2. then its copied but its contained objects are not. The contained objects obj-1 and obj-2 are affected by changes to cloned obj-2. java supports shallow cloning of objects by default when a class implements the java.lang.Cloneable  interface.

Deep copy:
            If a deep copy is performed on obj-1 as shown in fig-3 then not only obj-1 has been copied but the objects contained within it have been copied as well. Serialization can be used to achieved deep cloning. Deep cloning through serialization is faster to develop and easier to maintain but carries a performance overhead.
Ex: invoking clone() method on a collection like HashMap, List etc returns a shallow copy of HashMap, List instances. This means if you clone a HashMap, the map instance is cloned but the keys and values themselves are not cloned. If you want a deep copy then a simple method is to serialize the HashMap to a ByteArrayOutputStream and then deserialize it. This creates a deep copy but does require that all kes and values in the hashMap are Serializable. Main advantage of this is that is will deep copy any arbitrary values graph. Alternatively you can provide a static factory method to deep copy.

No comments:

Post a Comment

Post your comments.