Re: Mutable Dimension, argh!
"visionset" <spam@ntlworld.com> wrote in message
news:TbeRh.487$vo2.107@newsfe3-gui.ntli.net...
"Lew" <lew@nospam.lewscanon.com> wrote in message
news:vt6dnSoQK_lB84jbnZ2dnUVZ_hjinZ2d@comcast.com...
visionset wrote:
Mutable Dimension
Is there anything you do to minimise the impact of this sun gaf?
Or indeed any immutable where you have to use that Type.
Defensive copying.
See Joshua Bloch's /Effective Java/ - there's a chapter on this.
I know how to make my own classes. And yes that is my Bible!
I have to have Dimension type. I can subclass it and throw Unsupporteds
and yes do defensive copying.
But then it's a bit unsafe runtime wise when that lurking call to setXXX
kicks you.
Have I missed something obvious?
I believe MutableFoo and ImmutableFoo should not extend each other.
They fail the IS-A test in both directions (that is, it is not the case
that a MutableFoo IS-A ImmutableFoo, and vice versa).
For cases where an inheritance hierarchy is desirable, I'd use 3
classes (or 1 interface and 2 classes).
public interface Foo {
public int getX();
}
public class ImmutableFoo implements Foo {
private final int x;
public ImmutableFoo(final int x) {
this.x = x;
}
public int getX() {
return this.x;
}
public MutableFoo toMutableFoo() {
MutableFoo returnValue = new MutableFoo();
returnValue.setX(this.getX());
return returnValue;
}
}
public class MutableFoo implements Foo {
private int x;
public int getX() {
return this.x;
}
public int setX(final int x) {
this.x = x;
}
public ImmutableFoo toImmutableFoo() {
return new ImmutableFoo(this.getX());
}
}
- Oliver