You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gregor Zeitlinger <gr...@zeitlinger.de> on 2006/10/29 14:24:33 UTC

[lang] Cloneable replacement/CloneBuilder

1)
Since Cloneable is broken in Java, I think it would be a good idea to
introduce a better solution. The simplest solution would be

public interface Copyable extends Cloneable {
  public Object clone();
}

This would get rid of the problem that the clone method is not part of
the Cloneable interface.

I doesn't solve the problem that it would be nice to require that the
cloned object is exactly the same type:

public this clone();

but this isn't possible in Java.

2)
I would welcome a utility class that assists in implementing clone:
CloneBuilder
I don't really know the best way to do this, but it is important to
keep inheritance in mind.

public interface Copyable extends Cloneable {
  public Object clone();
  public Object clone(Object result);
}

public class A implements Copyable {
..
}
public class B extends A {
  int value;

  public Object clone() {
    return clone(new B());
  }
  public Object clone(Object result) {
    super.clone(result);
    B other = (B) result;
    other.value = value;
  }
}

The CloneBuilder would preferably make this simpler.

-- 
Gregor Zeitlinger
gregor@zeitlinger.de

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Cloneable replacement/CloneBuilder

Posted by Stephen Colebourne <sc...@btopenworld.com>.
We already have SerializationUtils.clone() to help with cloning.

I can't see how a CloneBuilder would help. Each clone is a very class 
specific function, and typically needs to be written directly within the 
class itself.

There just might be a role for a public clone interface, although it 
really needs to be in the JDK to be of any use.

Stephen


Gregor Zeitlinger wrote:
> 1)
> Since Cloneable is broken in Java, I think it would be a good idea to
> introduce a better solution. The simplest solution would be
> 
> public interface Copyable extends Cloneable {
>  public Object clone();
> }
> 
> This would get rid of the problem that the clone method is not part of
> the Cloneable interface.
> 
> I doesn't solve the problem that it would be nice to require that the
> cloned object is exactly the same type:
> 
> public this clone();
> 
> but this isn't possible in Java.
> 
> 2)
> I would welcome a utility class that assists in implementing clone:
> CloneBuilder
> I don't really know the best way to do this, but it is important to
> keep inheritance in mind.
> 
> public interface Copyable extends Cloneable {
>  public Object clone();
>  public Object clone(Object result);
> }
> 
> public class A implements Copyable {
> ..
> }
> public class B extends A {
>  int value;
> 
>  public Object clone() {
>    return clone(new B());
>  }
>  public Object clone(Object result) {
>    super.clone(result);
>    B other = (B) result;
>    other.value = value;
>  }
> }
> 
> The CloneBuilder would preferably make this simpler.
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org