关于不可变的阶级防御性复制 [英] Regarding an Immutable class defensive copying

查看:138
本文介绍了关于不可变的阶级防御性复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于创建不可变类的查询.以下是我要考虑的几点:

I have a query regarding creating a Immutable class. Following are the points which I take in consideration:

  1. 使班级最终
  2. 使所有成员成为最终成员,在静态块或构造函数中明确设置它们
  3. 将所有成员设为私人
  4. 没有修改状态的方法
  5. 要非常小心地限制对可变成员组件的访问(请记住,该字段可能是final的,但该对象仍然可以是可变的.即private final Date imStillMutable)-有关更多信息,请参见防御性复制或其表亲复制构造函数.


但是我一点都不完全理解5分,请您指教一下,或给我看一个例子,其中5分在那个例子中很清楚?


But I did not understand the 5 point completely at all, could you please advise or show me a example in which the 5 point is clear in that example?

推荐答案

第5点建议,只要您有任何可以返回与可变对象相关的方法,您就希望创建一个独立于私有状态的副本.例如:

Point 5 suggests that any time you have any methods which would return something to do with a mutable object, you'd want to create a copy which is independent of the private state. For example:

public final class Foo
{
    private final List<String> strings;

    public Foo(List<String> strings)
    {
        // Defensive copy on construction. Protects from constructing
        // code mutating the list.
        this.strings = new ArrayList<String>(strings);
    }

    public List<String> getStrings()
    {
        // Defensive copy on read. Protects from clients mutating the list.
        return new ArrayList<String>(strings);
    }
}

请注意,仅当状态可变时才需要进行防御性复制.例如,如果您在上面的类中使用ImmutableList(例如来自Guava的状态),则需要在构造上创建一个新列表(除非输入也是ImmutableList),但则不是getStrings中.

Note that the defensive copying is only required when the state is mutable. For example, if you used an ImmutableList (e.g. from Guava) as the state in the above class, you'd need to create a new list on construction (unless the input is also an ImmutableList) but not in getStrings.

还要注意,在这种情况下,String是不可变的,因此我们不需要复制每个字符串.如果这是List<StringBuilder>,我们需要创建一个新列表和每个元素的新副本作为防御副本的一部分.如您所见,当您所有的状态也都是不可变的时,生活变得更加简单.

Also note that in this case String is immutable, so we don't need to copy each string. If this were a List<StringBuilder> we'd need to create a new list and a new copy of each element as part of the defensive copy. As you can see, life becomes simpler when all your state is immutable too.

这篇关于关于不可变的阶级防御性复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆