`
iloveflower
  • 浏览: 79741 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

Immutable objects

 
阅读更多
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
分享到:
评论

相关推荐

    不可变对象ImmutableXXX:Collection、List、Set、Map…

    在Java编程中,不可变对象(Immutable Objects)是一种一旦创建后就不能被修改的对象。这种特性在多线程环境中尤其重要,因为不可变对象是线程安全的,它们不会因为并发修改而引发数据不一致的问题。Guava库是Google...

    ImmutableObjects

    ### Immutable Objects in Java 在Java编程语言中,不可变对象(Immutable Objects)是一个重要的概念,尤其是在构建健壮、易于维护的应用程序时。本篇将基于提供的文件内容来深入探讨不可变对象的概念及其在Java中...

    Get Programming - Learn to code with Python.epub

    Lesson 24 - Mutable and immutable objects Lesson 25 - Working with lists Lesson 26 - Advanced operations with lists Lesson 27 - Dictionaries as maps between objects Lesson 28 - Aliasing and copying ...

    java代码审查.pdf

    - 对于不可变对象(immutable objects)的错误使用,比如String类的错误使用方式。在Java中,String对象是不可变的,一旦创建就无法更改。文档中展示了错误地对String对象进行操作的实例,这是由于对Java中引用类型...

    Java理论与实践:修复Java内存模型2

    在并发编程中,不变对象(immutable objects)扮演着重要角色,因为它们天然具备线程安全性。JSR 133的改变确保了,一旦对象的final字段在构造函数中被初始化,其他线程将始终能看到这些初始化的值。这提高了不变...

    java多线程设计

    本知识点将深入探讨Java多线程设计以及如何利用“不可变对象”(immutable objects)来避免多线程环境中的非安全问题。 一、Java多线程基础 1. 线程的创建:Java提供了两种创建线程的方式——继承Thread类和实现...

    Java并发编程学习笔记

    - 不可变对象(Immutable Objects):一旦创建就不能被修改的对象,天然线程安全。 学习Java并发编程,重点在于理解线程如何协同工作,以及如何确保并发执行的正确性和高效性。通过深入掌握Java并发API,可以为大型...

    麻省理工18年春软件构造课程阅读12-接口与枚举-1

    此外,还提到了不可变对象(Immutable Objects)的概念,例如`ImmutableSquare`和`ImmutableRectangle`。不可变对象一旦创建就不能改变其状态,它们提供了安全性和并发性优势。`ImmutableSquare`继承自`...

    JAVA面试题汇总.pdf

    11. 在 Java 中不可变对象(Immutable Objects): - 一旦被创建,状态不能改变。 - 优点是线程安全。 - 示例:String、Integer。 12. Java 中的 switch 语句: - 在 JDK 1.7 之后支持 String 类型参数。 13. ...

    effective-java 配套代码

    4. **可变与不可变对象(Mutable vs Immutable Objects)**: 通过示例代码展示了如何创建不可变对象,以及不可变对象的益处和实现策略。 5. **泛型(Generics)**: 书中深入讲解了Java泛型的用法,包括类型擦除、...

    Java中堆和栈的区别

    例如,对于不变对象(immutable objects),如String,多次创建相同的字符串会导致内存浪费。为了避免这种情况,可以使用StringBuffer或StringBuilder类(在多线程环境下使用StringBuffer,单线程环境下使用...

    java 第四版英文.pdf.zip

    8. **可变与不可变对象(Mutable vs Immutable Objects)**:书中探讨了如何创建和使用不可变对象,以及它们在并发编程中的优势。 9. **多线程编程(Multithreaded Programming)**:Java提供了丰富的并发工具,如`...

    软件开发编码规范收集.pdf

    - 优先使用不可变对象(immutable objects),以减少数据篡改的风险。 - 序列化时要清除敏感信息,防止敏感数据在序列化过程中暴露。 6. **访问控制和保护**: - 尽可能将公共静态变量设为final,以防止未经授权...

    麻省理工18年春软件构造课程阅读09-避免调试-1

    4. **不可变对象(Immutable Objects)**:不可变对象在其创建后无法改变状态。在Java中,字符串是不可变的,这意味着一旦创建,就不能更改其内容。 5. **断言(Assertions)**:Java中的断言机制(`assert`关键字...

    JAVA变还是不变1

    在Java编程中,不变对象(Immutable Objects)是指一旦创建后,其状态就无法改变的对象。这种设计模式在很多情况下都带来了显著的好处。Java标准库中的String、Integer和BigDecimal类就是不变对象的典型例子,它们...

    effectice_java第二版 英文

    14. **条目14:优先考虑使用不可变对象( Prefer Immutable Objects)** 不可变对象简化并发编程,减少错误,并且可以作为线程安全的缓存项。 15. **条目15:最小化可变对象的范围(Minimize Mutability)** 尽...

    Java理论与实践:描绘线程安全性

    首先,我们来看不可变对象(Immutable Objects),这类对象一旦创建,其状态就不能改变,因此它们天然地线程安全。例如,`java.lang.String`就是不可变的,多个线程可以安全地共享同一个字符串实例。 其次,线程...

    java英文笔试

    **Answer**: In Java, `String` objects are immutable, meaning their values cannot be changed once they are created. On the other hand, `StringBuffer` objects are mutable, allowing you to modify their ...

    java的静态域实例域

    - 对于不变对象(immutable objects),所有域都是final和静态的,这样可以提高性能并防止意外修改。 总之,理解和熟练运用Java的静态域和实例域以及它们与构造方法的关系是Java程序员的基本技能。通过合理的设计和...

    Java 并发核心编程

    不可变字段与对象(Final fields & Immutable objects) 不可变字段(final fields)在对象创建后必须被赋予一个值,此后其值不可更改。不可变字段的设定值对所有线程可见,即使在无同步的情况下也是如此。不可变...

Global site tag (gtag.js) - Google Analytics