不可改变的阶级? [英] Immutable class?

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

问题描述

如何使Java类不可变,不可变性的需要是什么,使用它有什么好处?

How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?

推荐答案

什么是不可变对象?

不可变对象是在实例化后不会改变状态的对象。

An immutable object is one that will not change state after it is instantiated.

如何使对象不可变?

通常,可以通过定义来创建不可变对象没有任何成员公开的类,并且没有任何setter。

In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters.

以下类将创建一个不可变对象:

The following class will create an immutable object:

class ImmutableInt {
  private final int value;

  public ImmutableInt(int i) {
    value = i;
  }

  public int getValue() {
    return value;
  }
}

从上面的例子中可以看出,值只能在实例化对象时设置 ImmutableInt ,并且只有一个getter( getValue )对象的实例化后不能更改状态。

As can be seen in the above example, the value of the ImmutableInt can only be set when the object is instantiated, and by having only a getter (getValue) the object's state cannot be changed after instantiation.

但是,必须注意对象引用的所有对象也必须是不可变的,或者可以改变对象的状态。

However, there must be care taken that all objects that are referenced by the object must be immutable as well, or it could be possible to change the state of the object.

例如,允许通过getter获取对数组的引用或 ArrayList 将允许内部状态通过更改数组或集合进行更改:

For example, allowing an reference to an array or ArrayList to be obtained through an getter will allow the internal state to change by changing the array or collection:

class NotQuiteImmutableList<T> {
  private final List<T> list;

  public NotQuiteImmutableList(List<T> list) {
    // creates a new ArrayList and keeps a reference to it.
    this.list = new ArrayList(list); 
  }

  public List<T> getList() {
    return list;
  }
}

上面代码的问题是, ArrayList 可以通过 getList 获得并被操纵,导致对象本身的状态被改变,因此,不是不可变。

The problem with the above code is, that the ArrayList can be obtained through getList and be manipulated, leading to the state of the object itself to be altered, therefore, not immutable.

// notQuiteImmutableList contains "a", "b", "c"
List<String> notQuiteImmutableList= new NotQuiteImmutableList(Arrays.asList("a", "b", "c"));

// now the list contains "a", "b", "c", "d" -- this list is mutable.
notQuiteImmutableList.getList().add("d");

解决此问题的一种方法是从a调用时返回数组或集合的副本getter:

One way to get around this problem is to return a copy of an array or collection when called from a getter:

public List<T> getList() {
  // return a copy of the list so the internal state cannot be altered
  return new ArrayList(list);
}

不变性有什么好处?

不可变性的优势在于并发性。在可变对象中难以保持正确性,因为多个线程可能试图改变同一对象的状态,导致一些线程看到同一对象的不同状态,这取决于对所述读取和写入的时间的定时。对象。

The advantage of immutability comes with concurrency. It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object, leading to some threads seeing a different state of the same object, depending on the timing of the reads and writes to the said object.

通过拥有一个不可变对象,可以确保查看该对象的所有线程都将看到相同的状态,因为不可变对象的状态将是不要改变。

By having an immutable object, one can ensure that all threads that are looking at the object will be seeing the same state, as the state of an immutable object will not change.

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

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