Y
Yamin
Hey guys,
This is more of an interest question. To make deepCopying an object
easier, I make an interface "deepCopy". It has two function
public Object createDeepCopy(); //creates an new deep copy of the the
object
public void assignDeepCopy(Object n); //deep copy n to this
The general way to write these function for any type. Lets say
Type1,Type2 is:
class Type1 implements deepCopy
{
myType var1;
public Object createDeepCopy()
{
Type1 nc = new Type1();
nc.assignDeepCopy(this);
return nc;
}
public void assignDeepCopy(Object nc)
{
Type1 right = (Type1)nc;
this.varA = right.varA.createDeepCopy();
{
}
class Type2 extends Type1 implements deepCopy
{
myType var2;
int var3;
public Object createDeepCopy()
{
Type2 nc = new Type2();
nc.assignDeepCopy(this);
return nc;
}
public void assignDeepCopy(Object nc)
{
Type2 right = (Type2)nc;
super.assignDeepCopy(right);
this.var2 = right.var2.createDeepCopy();
this.var3 = right.var3;
{
}
That should be pretty clear of the intent. Using clone() in place of
createDeepCopy() might be an idea, but I think clone() is vague and
says its for shallow copies (in collections). This makes it safe to
subclass and what not. The problem occurs that this code is all very
repetitive. I've been thinking of ways to make this pattern more
mantainable.
Method1. make deepCopy a baseClass
public final Object createDeepCopy()
{
Object nc = this.getClass().newInstance();
nc.assignDeepCopy(this);
return nc;
}
problem. This really prevents classes from being retrofitted with
deepCopy due to the lack of multiple inheritance. For example, you
could not simply subclass off Hashtable to get a deepcopiable
hashtable.
Method 2: use java query methods to auto copy the field
public final void assignDeepCopy(Object nc)
{
//use java reflection and Field to auto copy all members
//I know its possible, but I haven't wrote up the code yet
{
potential problems: you'd have to either make sure all elements are
deepCopy, or write a big tester that checks the type. Special cases
for primitives, Strings, java collections...I'm still doing these
normally now inside each assignDeepCopy.
Any thoughts on this or other good design patterns.
Thanks,
Yamin
This is more of an interest question. To make deepCopying an object
easier, I make an interface "deepCopy". It has two function
public Object createDeepCopy(); //creates an new deep copy of the the
object
public void assignDeepCopy(Object n); //deep copy n to this
The general way to write these function for any type. Lets say
Type1,Type2 is:
class Type1 implements deepCopy
{
myType var1;
public Object createDeepCopy()
{
Type1 nc = new Type1();
nc.assignDeepCopy(this);
return nc;
}
public void assignDeepCopy(Object nc)
{
Type1 right = (Type1)nc;
this.varA = right.varA.createDeepCopy();
{
}
class Type2 extends Type1 implements deepCopy
{
myType var2;
int var3;
public Object createDeepCopy()
{
Type2 nc = new Type2();
nc.assignDeepCopy(this);
return nc;
}
public void assignDeepCopy(Object nc)
{
Type2 right = (Type2)nc;
super.assignDeepCopy(right);
this.var2 = right.var2.createDeepCopy();
this.var3 = right.var3;
{
}
That should be pretty clear of the intent. Using clone() in place of
createDeepCopy() might be an idea, but I think clone() is vague and
says its for shallow copies (in collections). This makes it safe to
subclass and what not. The problem occurs that this code is all very
repetitive. I've been thinking of ways to make this pattern more
mantainable.
Method1. make deepCopy a baseClass
public final Object createDeepCopy()
{
Object nc = this.getClass().newInstance();
nc.assignDeepCopy(this);
return nc;
}
problem. This really prevents classes from being retrofitted with
deepCopy due to the lack of multiple inheritance. For example, you
could not simply subclass off Hashtable to get a deepcopiable
hashtable.
Method 2: use java query methods to auto copy the field
public final void assignDeepCopy(Object nc)
{
//use java reflection and Field to auto copy all members
//I know its possible, but I haven't wrote up the code yet
{
potential problems: you'd have to either make sure all elements are
deepCopy, or write a big tester that checks the type. Special cases
for primitives, Strings, java collections...I'm still doing these
normally now inside each assignDeepCopy.
Any thoughts on this or other good design patterns.
Thanks,
Yamin