deep copy quesiton

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
 
A

Ann

Yamin said:
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.

Anything wrong with clone() ??
 
Y

Yamin

<snip>
Anything wrong with clone() ??

Yes, clone does not do deepCopy() in collection.
Also, how do you handle subclasses nicely with clone?


Yamin
 
A

Ann

Yamin said:
<snip>
Anything wrong with clone() ??

Yes, clone does not do deepCopy() in collection.
Also, how do you handle subclasses nicely with clone?


Yamin

Is it possible to subclass clone() ???
 
E

Esmond Pitt

Yamin said:
The problem occurs that this code is all very
repetitive. I've been thinking of ways to make this pattern more
mantainable.

This is a prime example for the use of JDK 1.5 Generics.
 
M

Michiel Konstapel

Any thoughts on this or other good design patterns.

The easy way to deep copy is to serialize to a byte array, then
deserialize. Of course, your objects have to be Serializable for this to
work.
Michiel
 
Y

Yamin

(e-mail address removed) (Yamin) wrote in message <snip>

Thanks for the help all.
I never thought of the serialization method. It seems like an
interesting way to do it.

Yamin
 
C

charely

(e-mail address removed) (Yamin) wrote in message
<snip>

Thanks for the help all.
I never thought of the serialization method. It seems like an
interesting way to do it.

Yamin

this should work for serializable objects.

public static Object deepCopy(Object obj) {
try {
ByteArrayOutputStream byteOutStream = new
ByteArrayOutputStream();
ObjectOutputStream objOutStream = new
ObjectOutputStream(byteOutStream);
objOutStream.writeObject(obj);
ByteArrayInputStream byteInStream = new
ByteArrayInputStream(byteOutStream.toByteArray());
ObjectInputStream objInStream = new
ObjectInputStream(byteInStream);
return objInStream.readObject();
} catch (IOException e) {
...
} catch (ClassNotFoundException e) {
...
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top