I'm currently implementing iterative solvers, which work by successively improving the estimate the solution to a specific problem. As the solution is a rather large set of data, refinement is carried out in place.
I have implemented a simple Observer/Observable pattern in order to be able to watch the algorithm while the iterations take place. In particular, the solver provides a method
Foo getCurrentSolution()
which returns the current estimate of the solution. The observer is then free to do some computations, based on the current estimate (for example: to decide whether or not the solution is good enough and the iterations could be stopped). Foo
is mutable, but of course, if the observer modifies the current estimate of the solution, this can ruin the iterations of the solver.
Therefore, getCurrentSolution()
should really return a defensive copy. But this requires time and memory on large problems, so I came up with another idea, which is to have getCurrentSolution()
return a new ReadOnlyFoo(bar)
, where foo
is the (mutable) current estimate of the solution, private to the solver. The idea is that ReadOnlyFoo
has almost the same interface as Foo
, only the methods which might modify the data are "deactivated" (they throw an exception). All details of some dummy classes are given below.
My question is: is this approach good practice? Is there a better pattern?
Thanks! Sebastien
public abstract class AbstractFoo{
public abstract double getValue();
public abstract void setValue(final double x);
public abstract AbstractFoo add(AbstractFoo bar);
public void addToSelf(AbstractFoo bar){
setValue(getValue + bar.getValue());
}
}
public class Foo extends AbstractFoo{
private double value;
public Foo(final double x){
value = x;
}
public double getValue(){
return value;
}
public void setValue(final double x){
value = x;
}
public AbstractFoo add(AbstractFoo bar){
return new Foo(value + bar.getValue());
}
}
public final class FooReadOnly extends AbstractFoo{
private final Foo foo;
public FooReadOnly(AbstractFoo foo){
this.foo = foo;
}
public double getValue(){
return foo.getValue();
}
public void setValue(final double x){
throw new NotImplementedException("read only object");
}
public AbstractFoo add(AbstractFoo bar){
return foo.add(bar);
}
public void addToSelf(AbstractFoo bar){
throw new NotImplementedException("read only object");
}
}