commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gregor Zeitlinger" <gre...@zeitlinger.de>
Subject [lang] Cloneable replacement/CloneBuilder
Date Sun, 29 Oct 2006 13:24:33 GMT
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


Mime
View raw message