• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the need for cloning a object ?

 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the need for cloning a object ? give me a practial example.

so that i can understand very deeply.

what is the advantage of clonning ?
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes you need to do a calculation on a dataset, where the dataset will be modified. If you only want the result and not modify the dataset, cloning that data is needed. This way, your calculation can work on a copy of the dataset.

Henry
[ October 31, 2007: Message edited by: Henry Wong ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are times when initializing an object could be too expensive in terms of the resources that such initialization may consume. Therefore, instead of creating a new object, you can clone an existing one and only change the properties that matter to your new instance.

A variation of this last suggestion would be when an object has too many properties, and initializing is too complicated. Therefore, you could initialize a single object, and then reset the properties that matter in every clone.

You might like to study a bit of the Prototype Pattern which seems to use the principle of cloning.

I hope this helps!
[ October 31, 2007: Message edited by: Edwin Dalorzo ]
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another important use case is when a class must provide an API that returns a complex object (such as a List) that you do not want to allow the caller to modify. So you can make a clone, and return that. The user/caller can change the clone without changing your internal important list.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warning! Clones created by the Object clone method are "shallow" clones. References to other objects are duplicated so both original and clone references point to the same object (for example, a List and items on a List). Therefore using a clone does NOT guarantee that your original object will be protected from modification.

Thats why people write custom clone methods.

Bill
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bill]: Therefore using a clone does NOT guarantee that your original object will be protected from modification.

Or to put it another way, it can guarantee protection from modification, but only if you are sure that the elements within your array/list/collection/whatever are immutable objects. Otherwise you need a deep copy, which generally requires a custom method to loops through and perform whatever action is appropriate to copy the contents.
[ November 01, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic