将可变对象封装为只读对象
我目前正在实现迭代求解器,它通过连续改进对特定问题的解决方案的估计来工作。由于解决方案是一个相当大的数据集,因此进行了适当的细化。
我实现了一个简单的观察者/可观察模式,以便能够在迭代发生时观察算法。特别是,求解器提供了一种
Foo getCurrentSolution()
返回解的当前估计值的方法。然后观察者可以根据当前的估计自由地进行一些计算(例如:决定解决方案是否足够好并且可以停止迭代)。 Foo
是可变的,但是当然,如果观察者修改了解的当前估计,这可能会破坏求解器的迭代。
因此,getCurrentSolution()
确实应该返回一个防御性副本。但这需要时间和内存来解决大问题,所以我想出了另一个想法,那就是让 getCurrentSolution()
返回一个新的 ReadOnlyFoo(bar)
,其中 >foo
是解的(可变的)当前估计,对求解器来说是私有的。这个想法是 ReadOnlyFoo
与 Foo
具有几乎相同的接口,只有可能修改数据的方法被“停用”(它们抛出异常)。下面给出了一些虚拟类的所有详细信息。
我的问题是:这种方法是好的做法吗?有更好的模式吗?
谢谢! 塞巴斯蒂安
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");
}
}
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将定义一个仅包含只读方法的接口
Solution
和一个包含所有方法的可变类MutableSolution
,并创建getCurrentSolution()
> 方法返回一个Solution
实例。这样,您不需要创建防御性副本或将解决方案包装到只读包装器中。当然,观察者仍然可以将解决方案转换为
MutableSolution
,但这并非偶然。如果您想保护自己免受强制转换,请编写一个实现Solution
的ReadOnlySolution
包装类,并将其委托给包装的MutableSolution
。这与您的命题类似,只是方法的签名清楚地表明该对象是不可变的。I would define an interface
Solution
containing only the read-only methods, and a mutable classMutableSolution
containing all the methods, and make thegetCurrentSolution()
method return aSolution
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 aReadOnlySolution
wrapper class implementingSolution
and delegating to the wrappedMutableSolution
. This is similar to your proposition, except that the signature of the method makes it clear that the object is not mutable.这实际上是
Collections
类使用unmodifyingList(...)
等的方法。它返回一个包含原始列表的包装器,但在修改列表的方法中抛出异常。收藏。That's actually the approach that the
Collections
class does withunmodifiableList(...)
etc. It returns a wrapper that contains the original list but throws exceptions in the methods that modify the collection.我不会那样做。如果一个人甚至使用一个通用接口(可能存在于您的实际实现中)的
AbstractFoo
,那么他事先并不知道当前实例是否可变。因此,用户将面临抛出一些未经检查的异常的风险。而且,对于一个不可变的对象来说,不可修改它一点也不例外。换句话说:我不会使用 execption 来表示试图修改 FooReadOnly 的实例。
至少我会向抽象类
AbstractFoo
添加一个抽象方法boolean isModabilible()
,以便我们可以测试是否可以修改目的。在这种情况下,我们不需要抛出异常 - 修改方法的实现可以简单地什么都不做。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 classAbstractFoo
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.为什么要采用这种过度设计的解决方案?为什么不拥有一个类和 readOnly 布尔属性?然后为每个 setter 执行 checkWriteable() 。
Why do such an overengineered solution? Why not have one class and readOnly boolean attribute? Then do checkWriteable() for each setter.