繁体   English   中英

不可变对象的所有属性都必须是最终的吗?

[英]Must all properties of an immutable object be final?

不可变对象必须所有属性都是final的吗?

我会说他们没有。 但我不知道我是否正确。

不可变对象(所有属性最终)和有效不可变对象(属性不是最终但不能更改)之间的主要区别是安全发布。

您可以在多线程上下文中安全地发布不可变对象,而不必担心添加同步,这要归功于Java 内存模型为 final 字段提供的保证

final 字段还允许程序员在不同步的情况下实现线程安全的不可变对象。 线程安全的不可变对象被所有线程视为不可变的,即使使用数据竞争在线程之间传递对不可变对象的引用也是如此。 这可以提供安全保证,防止错误或恶意代码滥用不可变类。 必须正确使用 final 字段以保证不变性。

作为旁注,它还可以强制执行不变性(如果您尝试在未来版本的类中改变这些字段,因为您忘记了它应该是不可变的,它不会编译)。


澄清

  • 将对象的所有字段设为 final 并不会使其不可变 - 您还需要确保 (i) 它的状态不会改变(例如,如果对象包含final List ,则不会发生变异操作(添加、删除。 ..) 必须在施工后完成)和 (ii) this施工期间不要让它逃跑
  • 一个有效的不可变对象在安全发布后是线程安全的
  • 不安全发布示例:

     class EffectivelyImmutable { static EffectivelyImmutable unsafe; private int i; public EffectivelyImmutable (int i) { this.i = i; } public int get() { return i; } } // in some thread EffectivelyImmutable.unsafe = new EffectivelyImmutable(1); //in some other thread if (EffectivelyImmutable.unsafe != null && EffectivelyImmutable.unsafe.get() != 1) System.out.println("What???");

    这个程序理论上可以打印What??? . 如果i是最终的,那将不是法律结果。

您可以仅通过封装轻松保证不变性,因此没有必要

// This is trivially immutable.
public class Foo {
    private String bar;
    public Foo(String bar) {
        this.bar = bar;
    }
    public String getBar() {
        return bar;
    }
}

但是,在某些情况下,您还必须通过封装来保证它,所以这还不够

public class Womble {
    private final List<String> cabbages;
    public Womble(List<String> cabbages) {
        this.cabbages = cabbages;
    }
    public List<String> getCabbages() {
        return cabbages;
    }
}
// ...
Womble w = new Womble(...);
// This might count as mutation in your design. (Or it might not.)
w.getCabbages().add("cabbage"); 

这样做是为了捕捉一些微不足道的错误,并清楚地展示你的意图,但“所有字段都是最终的”和“类是不可变的”不是等价的陈述。

不可变 = 不变。 所以使属性最终是一个好主意。 如果不是一个对象的所有属性都受到保护而不会被更改,我不会说该对象是不可变的。

但是,如果一个对象不为其私有属性提供任何设置器,它也是不可变的。

不可变对象在创建后不得以任何方式修改。 final 当然有助于实现这一目标。 您保证它们永远不会改变。 但是如果你的对象中有一个最终的数组怎么办? 当然,参考是不可变的,但元素是可变的。 看看我也给出的几乎相同的问题:

关联

简单地将对象声明为final并不会使其本质上是不可变的。 以这个为例:

import java.util.Date;

/**
* Planet is an immutable class, since there is no way to change
* its state after construction.
*/
public final class Planet {

  public Planet (double aMass, String aName, Date aDateOfDiscovery) {
     fMass = aMass;
     fName = aName;
     //make a private copy of aDateOfDiscovery
     //this is the only way to keep the fDateOfDiscovery
     //field private, and shields this class from any changes that 
     //the caller may make to the original aDateOfDiscovery object
     fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());
  }

  /**
  * Returns a primitive value.
  *
  * The caller can do whatever they want with the return value, without 
  * affecting the internals of this class. Why? Because this is a primitive 
  * value. The caller sees its "own" double that simply has the
  * same value as fMass.
  */
  public double getMass() {
    return fMass;
  }

  /**
  * Returns an immutable object.
  *
  * The caller gets a direct reference to the internal field. But this is not 
  * dangerous, since String is immutable and cannot be changed.
  */
  public String getName() {
    return fName;
  }

//  /**
//  * Returns a mutable object - likely bad style.
//  *
//  * The caller gets a direct reference to the internal field. This is usually dangerous, 
//  * since the Date object state can be changed both by this class and its caller.
//  * That is, this class is no longer in complete control of fDate.
//  */
//  public Date getDateOfDiscovery() {
//    return fDateOfDiscovery;
//  }

  /**
  * Returns a mutable object - good style.
  * 
  * Returns a defensive copy of the field.
  * The caller of this method can do anything they want with the
  * returned Date object, without affecting the internals of this
  * class in any way. Why? Because they do not have a reference to 
  * fDate. Rather, they are playing with a second Date that initially has the 
  * same data as fDate.
  */
  public Date getDateOfDiscovery() {
    return new Date(fDateOfDiscovery.getTime());
  }

  // PRIVATE //

  /**
  * Final primitive data is always immutable.
  */
  private final double fMass;

  /**
  * An immutable object field. (String objects never change state.)
  */
  private final String fName;

  /**
  * A mutable object field. In this case, the state of this mutable field
  * is to be changed only by this class. (In other cases, it makes perfect
  * sense to allow the state of a field to be changed outside the native
  * class; this is the case when a field acts as a "pointer" to an object
  * created elsewhere.)
  */
  private final Date fDateOfDiscovery;
}

不。

例如,查看java.lang.String的实现。 字符串在 Java 中是不可变的,但字段hash不是最终的(它是在第一次调用hashCode时延迟计算然后缓存的)。 但这是可行的,因为hash在每次计算时只能采用一个相同的非默认值。

字符串类是不可变的,但属性哈希不是最终的

好吧,这是可能的,但有一些规则/限制,即访问可变属性/字段必须在我们每次访问它时提供相同的结果。

在 String 类中,哈希码实际上是根据最终的字符数组计算的,如果 String 已构造,则不会更改。 因此,不可变类可以包含可变字段/属性,但它必须确保每次访问字段/属性时都会产生相同的结果。

要回答您的问题,不必将所有字段都放在不可变类中。

如需进一步阅读,请访问此处 [博客]:http: //javaunturnedtopics.blogspot.in/2016/07/string-is-immutable-and-property-hash.html

没有必要,您可以通过将成员设为非最终但私有并且除了在构造函数中之外不修改它们来实现相同的功能。 不要为他们提供 setter 方法,如果它是一个可变对象,那么永远不要泄漏该成员的任何引用。

请记住,将引用变量设置为 final,仅确保不会重新为其分配不同的值,但您仍然可以更改该引用变量指向的对象的各个属性。 这是关键点之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM