3

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");
    }
}
Sebastien
  • 246
  • 1
  • 12

4 Answers4

3

I would define an interface Solution containing only the read-only methods, and a mutable class MutableSolution containing all the methods, and make the getCurrentSolution() method return a Solution instance. This way, you don't need to create a defensive copy or to wrap your solution into a read-only wrapper.

Of course, the observer could still cast the solution to a MutableSolution, but this wouldn't be an accident. If you want to protect yourself against casts, then write a ReadOnlySolution wrapper class implementing Solution and delegating to the wrapped MutableSolution. This is similar to your proposition, except that the signature of the method makes it clear that the object is not mutable.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • That would definitely be cleaner, but assume I cannot change the implementation of `Solution`? I can't make it inherit from `MutableSolution`... – Sebastien Aug 11 '11 at 11:14
  • Then define an interface, with the read-only methods only, which is returned by getCurrentSolution, and define a class implementing this interface and delegating to your mutable object. – JB Nizet Aug 11 '11 at 11:21
  • I am not satisfied with the other option, so I think I'll go for this one. Thanks a lot. – Sebastien Aug 11 '11 at 13:33
1

That's actually the approach that the Collections class does with unmodifiableList(...) etc. It returns a wrapper that contains the original list but throws exceptions in the methods that modify the collection.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • I wasn't aware of that. I'll look into it. Someone raised this problem though: if you add a new mutator to the wrapped object, this approach is no longer safe. – Sebastien Aug 11 '11 at 11:15
  • Right. This problem doesn't exist with my solution. – JB Nizet Aug 11 '11 at 11:26
1

I wouldn't do that. If one uses AbstractFoo of even a common interface (which might exist in your real implementation), then he doesn't know in advance, if the current instance is mutable or not. So the user will risk some unchecked exceptions to be thrown.

And, for an immutable object, beeing unmodifiable it's not exceptional at all. In other words: I wouldn't use execption to signal, that one attempted to modify an instance of FooReadOnly.

At least I'd add an abstract method boolean isModifiable() to the abstract class AbstractFoo so that we can test, if we can modify the object. And in that case, we wouldn't need to throw exceptions - the implementation of the modifying methods could simply do nothing.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I would still make the methods throw exceptions, in order to make it clear that the developer didn't respect the contract: don't call setter methods if isModifiable is false. I would use the standard java.lang.UnsupportedOperationException, though. – JB Nizet Aug 11 '11 at 11:25
  • I never liked the `UnsupportedOperationException` trick that they do for example in `Iterator`. Suppose you receive a instance of `Iterator` from some method and want to use it to delete an element: you'll have to `try` and `catch` an unchecked exception just to learn if *this* iterator supports deleting elements. Not every line of code in the JRE is a masterpiece and worth to be copied ;) – Andreas Dolk Aug 11 '11 at 11:33
0

Why do such an overengineered solution? Why not have one class and readOnly boolean attribute? Then do checkWriteable() for each setter.

Rafal Rusin
  • 623
  • 6
  • 12
  • There should be a flag not only for `readOnly`, but also for `immutable`. They do *not* mean the same thing. Code which wants to know and immediately use the current state of the solution could be satisfied with a read-only wrapper to an object which will change in future, but code which wants a snapshot of the current state would need to make a defensive copy of the object it was given *unless it was already known to be immutable*. It's too bad there's so little support in many libraries for objects to report whether or not they're immutable. – supercat Sep 19 '12 at 15:40