In-depth shallow Java Clone technology (3)
This chapter will enter the advanced characteristics of Clone, focusing on depth Clone technology.
Clone usually has two types of shallow Clone and deep clone. First, analyze the differences in two. Shallow Clone and Deep Clone are Clone, which essentially defines whether the subject's property (non-primary type attribute, such as INT, etc.) is derived at Clone. If it is still reserved as a reference, it is called shallow Clone, which is called a deep clone. In fact, these two concepts are also relative concepts. They have a bit different from them, and the shallow clone mode is a Clone object. After getting the Clone object, "Clone" is required to "Clone" processing on the referenced member properties. From this level, the deep clone does not have a special difficulty. Simple talk is to create a good object and set some member properties. About deep clone, there are too many articles on the Internet, and it is a bit of a bitter feeling. This article is no longer described. This is not the focus of this article.
The focus of this article is to explain the depth clone, that is, "N Deep Clone". What exactly? The goal described herein is to have been deep to the extent you want, including deeper deeper.
Implementation is combined with Java Reflection technology and recursive.
The approximate step is as follows: First, Java Reflection technology is used to dynamically obtain a list of members. Then via the depth of Clone, the members with Clone conditions and the necessary Clone are also clone. This is a recursive process until the Clolne depth has until the object does not require Clone's member properties.
What is a member of Clone condition and a member of the necessary Clone? For example, the primitive type, the type of transient, is not accessible (! Field # isaccessible ()), does not implement the type of the Cloneable interface, etc., there is no Clone condition. The type of Java definition such as String does not need to go deep into Clone, which belongs to unnecessary clone. But the "container" class such as the List type is a member type of the clone.
According to this, the recursive program is illustrated (DeepClone is a Java method):
/ **
* @Return Object Returns the object of Clone
* @Param Obj original object
* @Param Length Clone's depth
* /
Public Object DeepClone (Object Obj, INT Length) {
Object result = OBJ;
// This is pseudo code: If the object OBJ does not have a Clone condition, return Result, which is also the recursive end condition.
/ / Here is pseudo code: If the object OBJ is not necessary, returns Result
// This is pseudo code: Start "Clone" object. This place is to adjust an abstract method to add a lot of flexibility. This method is the main purpose to implement the "clone" object scheme. Note: The "Clone" program in this may be what we want to think of, it may have a lot of ideas, but the effect is the same, it is to "clone" new object. Of course, it is the most easy thinking that the Object # clone () method is. Drawing is as follows:
Result = om.clone (OBJ);
// Here is pseudo code: all members who have a Clone condition and the necessary Clone are available. This place is also an abstract method to handle it. It is also to enhance flexibility. There are many ways to get these members, which may be obtained by setter and getter pairs, or by get Fields, etc. (this method may be many members can be directly accessible, often need to be combined with other methods), Even a combination of a variety of methods. In short, the purpose is only one, that is, these members are obtained. For (int i = 0; i / / Treatment // If you have no need to judge the member, then in addition to the "container" member, it has also ended over IF (Length <= 1) { IF (! "" container "member) { CONTINUE; } Try { // Just clon once, pay attention to the depth parameters of the recursive method CloneDfieldValue = DeepClone ("Container" member, 1); } catch (exception ex2) { EX2.PRINTSTACKTRACE (); Return Result; } } else { Try { CloneDfieldValue = DeepClone (member of member, Length - 1); } catch (exception ex) { EX.PrintStackTrace (); Return Result; } } Try { / / Here is pseudo code: set the clone's good value (CloneDfieldValue) } catch (exception ex) { EX.PrintStackTrace (); Return Result; } } // for .. Return Result; } At this point, "N Deep Clone" has been completed. Let's discuss other related issues. For example, this depth Clone is originally a-> b -> c - ... -> XZ, that is, Class A contains B members, and the B is also included in C member, and so on. If you want to "N Deep Clone", what is the member of the clone "XZ"? In fact, it is very simple. This problem is mainly to solve some members in the recursive process. Some members do not need clone to still reserve the quote. In the above recursion example, it has been mentioned that "the" clone "" scheme "has been defined as an abstract method, then we only need to make this method to meet this demand implementation.