Immutable objects are simply objects whose state (the object's data) cannot change after construction.
Examples of immutable objects from the JDK include
String
and
Integer
.
Immutable objects greatly simplify your program, since they :
-
are simple to construct, test, and use
-
are automatically thread-safe
and have no synchronization
issues
-
do not need a copy constructor
-
do not need an implementation of clone
-
allow hashCode
to use lazy initialization
,
and to cache its return value
-
do not need to be copied defensively
when used
as a field
-
make good Map
keys and Set
elements (these objects must
not change state while in the collection)
-
have their class invariant
established once upon construction,
and it never needs to be checked again
- always have "failure atomicity" (a term used by Joshua Bloch) : if
an immutable object throws an exception, it's never left in an
undesirable or indeterminate state
Immutable objects have a very compelling(
令人信服的,有说服力的
) list of positive qualities.
Without question, they are among the simplest and most robust kinds of classes you can possibly build.
When you create immutable classes, entire categories of problems simply disappear.
Make a class immutable by following these guidelines :
- ensure the class cannot be overridden - make the class final
, or
use static factories and keep constructors private
- make fields private
and final
- force callers to construct an object completely in a single step, instead of using a no-argument constructor
combined with(与…结合
) subsequent calls to setXXX
methods (that is, avoid the Java Beans convention
)
- do not provide any methods which can change the state of the object in
any way - not just setXXX
methods, but any method which can change
state
- if the class has any mutable object fields, then they must be
defensively copied
when passed between the class and its caller
In Effective Java
, Joshua Bloch makes this compelling recommendation :
"Classes should be immutable unless there's a very good reason to
make them mutable....If a class cannot be made immutable, limit its mutability as much as possible."
It's interesting to note that
BigDecimal
is technically not
immutable, since it's not final
.
Example
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;
}
Note that javadoc
1.4
includes the -tag
option, whereby simple custom tags may
be defined. One might define an @is.Immutable
tag, for example,
to document a class as being immutable.
You might also consider defining your own tag interface
for immutable objects.
分享到:
相关推荐
在Java编程中,不可变对象(Immutable Objects)是一种一旦创建后就不能被修改的对象。这种特性在多线程环境中尤其重要,因为不可变对象是线程安全的,它们不会因为并发修改而引发数据不一致的问题。Guava库是Google...
在Java编程语言中,不可变对象(Immutable Objects)是一个重要的概念,尤其是在构建健壮、易于维护的应用程序时。本篇将基于提供的文件内容来深入探讨不可变对象的概念及其在Java中的应用。 #### 不可变对象定义 ...
本知识点将深入探讨Java多线程设计以及如何利用“不可变对象”(immutable objects)来避免多线程环境中的非安全问题。 一、Java多线程基础 1. 线程的创建:Java提供了两种创建线程的方式——继承Thread类和实现...
11. 在 Java 中不可变对象(Immutable Objects): - 一旦被创建,状态不能改变。 - 优点是线程安全。 - 示例:String、Integer。 12. Java 中的 switch 语句: - 在 JDK 1.7 之后支持 String 类型参数。 13. ...
在Java编程中,不变对象(Immutable Objects)是指一旦创建后,其状态就无法改变的对象。这种设计模式在很多情况下都带来了显著的好处。Java标准库中的String、Integer和BigDecimal类就是不变对象的典型例子,它们...
- 不可变对象(Immutable Objects):一旦创建就不能被修改的对象,天然线程安全。 学习Java并发编程,重点在于理解线程如何协同工作,以及如何确保并发执行的正确性和高效性。通过深入掌握Java并发API,可以为大型...
### 不可变对象(Immutable Objects) 在并发编程中,不可变对象是不可更改的对象,一旦创建了它的实例,就不能修改它的状态。不可变对象可以提供线程安全保证,因为它们的状态不会在多个线程间改变。 ### 高级...
4. **可变与不可变对象(Mutable vs Immutable Objects)**: 通过示例代码展示了如何创建不可变对象,以及不可变对象的益处和实现策略。 5. **泛型(Generics)**: 书中深入讲解了Java泛型的用法,包括类型擦除、...
- **不可变字符串**: 在这两种语言中,字符串对象一旦创建后,其值就不能改变。 9. **Unextendable Classes** - **不可扩展的类**: 两种语言都支持定义不可被继承的类。 10. **Throwing and Catching ...
8. **不可变对象 (Immutable Objects)**: - **描述**:不可变对象是指一旦创建就不能被改变的对象。在Java中,可以通过将对象的所有字段声明为`final`来创建不可变对象。不可变对象是线程安全的,因为它们的状态...
- 对于不可变对象(immutable objects)的错误使用,比如String类的错误使用方式。在Java中,String对象是不可变的,一旦创建就无法更改。文档中展示了错误地对String对象进行操作的实例,这是由于对Java中引用类型...
14. **条目14:优先考虑使用不可变对象( Prefer Immutable Objects)** 不可变对象简化并发编程,减少错误,并且可以作为线程安全的缓存项。 15. **条目15:最小化可变对象的范围(Minimize Mutability)** 尽...
不可变字段与对象(Final fields & Immutable objects) 不可变字段(final fields)在对象创建后必须被赋予一个值,此后其值不可更改。不可变字段的设定值对所有线程可见,即使在无同步的情况下也是如此。不可变...
在并发编程中,不变对象(immutable objects)扮演着重要角色,因为它们天然具备线程安全性。JSR 133的改变确保了,一旦对象的final字段在构造函数中被初始化,其他线程将始终能看到这些初始化的值。这提高了不变...
- **创建不可变对象**:另一种方法是设计不可变对象(Immutable Objects)。这意味着一旦对象被创建,它的状态就不能被更改。这通常通过使对象的所有字段成为final并且不允许任何外部修改操作来实现。这种方式可以...
8. **可变与不可变对象(Mutable vs Immutable Objects)**:书中探讨了如何创建和使用不可变对象,以及它们在并发编程中的优势。 9. **多线程编程(Multithreaded Programming)**:Java提供了丰富的并发工具,如`...
这涉及到了不可变对象(immutable objects)、线程安全对象(thread-safe objects)等概念,并且会给出一些具体的实现技巧。 ##### 1.3 设计力量 这部分内容深入探讨了并发编程中的“设计力量”(design forces)...
5. **不可变对象(Immutable Objects)** - 不可变对象是指一旦创建后其状态就不能被改变的对象。 - Java中的基本数据类型(如`int`、`long`)和String都是不可变的。 6. **Java内存模型** - Java内存模型规定了...
首先,我们来看不可变对象(Immutable Objects),这类对象一旦创建,其状态就不能改变,因此它们天然地线程安全。例如,`java.lang.String`就是不可变的,多个线程可以安全地共享同一个字符串实例。 其次,线程...
- 对于不变对象(immutable objects),所有域都是final和静态的,这样可以提高性能并防止意外修改。 总之,理解和熟练运用Java的静态域和实例域以及它们与构造方法的关系是Java程序员的基本技能。通过合理的设计和...