commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arne Ploese <aplo...@gmx.de>
Subject Re: [math] Read-only RealVector
Date Thu, 11 Aug 2011 08:46:39 GMT
So you not only want to observe the result, but you want a read only
RealVector.

A "clean" solution would be split RealVector in a base interface, which
not modifies any internal Data and a inherited interface which adds
xxxToSelf and setEnty(...). ??? I think this could lead to some
unforseeable side effects - so maybe some test implementation is
necessary to see whats happen.

Am Donnerstag, den 11.08.2011, 10:08 +0200 schrieb S├ębastien Brisard: 
> 2011/8/11 Arne Ploese <aploese@gmx.de>:
> > What methods do you need?
> >
> > Maybe an interface with:
> > public interface SimpleRealVector {
> >  double getEntry(int i);
> >  int getDimension();
> > }
> > will do?
> >
> No, I'd like to have *all* methods of the o.a.c.m.linear.RealVector
> interface, *except* those which modify the caller object, e.g.
> xxxToSelf and setXxx.
> My question was actually more general. To make an object unmodifiable,
> is it good practice to encapsulate it into another object, with
> seamingly the same interface, but unimplemented methods which might
> modify the object ? My point would be not to create any new
> interface/abstract class.
> 
> Below is a quick example (which I hope is not buggy).
> Thanks for your advice,
> S├ębastien
> 
> /**********/
> 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");
>   }
> }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
> 



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


Mime
View raw message