![]() This prevents the proliferation of objects, and heads off the special situation in which a circular reference produces an infinite loop of copies. Finally, even if the object being deep copied contains multiple references to another object, the latter object should still only be copied once. In this case, there must be two copy functions for each object: one for a deep copy and one for a shallow. (For example, Vector does not contain the logic necessary for a deep copy.) And if you want to write code that defers until runtime the question of whether to make a deep or shallow copy an object, you're in for an even more complicated situation. To make things even more complicated, if an object can't be modified directly and its clone() method produces a shallow copy, then the class must be extended, the clone() method overridden, and this new class used in place of the old. Getting this correct can be difficult and time consuming, and is rarely fun. To achieve a deep copy, extra logic must be added that explicitly calls all contained objects' clone() methods, which in turn call their contained objects' clone() methods, and so on. The default clone() method, inherited from Object, makes a shallow copy of the object. Each object is responsible for cloning itself via its clone() method. If either of these contained objects themselves contain objects, then, in a deep copy, those objects are copied as well, and so on until the entire graph is traversed and copied. Not only has obj1 been copied, but the objects contained within it have been copied as well. ![]() Figure 3 shows obj1 after a deep copy has been performed on it. After a shallow copy of obj1Ī deep copy occurs when an object is copied along with the objects to which it refers. ![]() If a shallow copy is performed on obj1, then it is copied but its contained objects are not, as shown in Figure 2. To illustrate, Figure 1 shows an object, obj1, that contains two objects, containedObj1 and containedObj2. To summarize briefly here, a shallow copy occurs when an object is copied without its contained objects. In a previous JavaWorld article, " How to avoid traps and correctly override methods from ," Mark Roulo explains how to clone objects as well as how to achieve shallow copying instead of deep copying. In order to understand what a deep copy is, let's first look at the concept of shallow copying. This article describes a simple technique to use in place of this time-consuming conventional deep copy. Traditionally, each class in the object must be individually inspected and edited to implement the Cloneable interface and override its clone() method in order to make a deep copy of itself as well as its contained objects. Implementing a deep copy of an object can be a learning experience - you learn that you don't want to do it! If the object in question refers to other complex objects, which in turn refer to others, then this task can be daunting indeed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |