不可变类:一旦创建,状态无法改变
关于创建不可变类有很多规则,下面一一介绍这些规则:
目录
- 定义不可变类的益处
- 定义不可变类指南
定义不可变的益处
- 构造简单,便于测试和使用
- 不可变类自然是线程安全的,无需关心多线程和同步问题
- 不需要实现clone
- 可以延迟加载,缓存它的返回值
- 由于不可变可以用于Map的key和Set的元素(set元素不能重复)
- 当作为属性时,不需要深度clone
如何让类不可变
在Java文档中,有关于如何定义不可变类指南: click here
- 不提供setter方法,setter方法用于修改属性和对象引用
这个原则阐述了在你类定义的所有可变属性中,不提供setter方法,setter方法意味着你能够改变这个属性的状态。必须阻止提供setter方法 - 所有的属性修饰添加private和final
这是另外一种增加不可变的方式,属性声明为private为了在类之外不能够被访问到,final修饰符为了让你不能随便的改变它们 - 不允许子类重写方法
最简单的方式声明类为final,final类不允许被重写 - 当属性中存在可变对象变量时,要特别留意
永远铭记你的对象变量,不是可变的就是不可变的(这句好像是废话。。),识别出来可变对象,对可变对象的内容进行copy,并创建一个新对象赋值给它,这样保证可变对象的不可变,通过直接copy对象内容的形式,保持数据不可变 来点优雅的,定义一个private的构造方法,通过 工厂方法构造对象
只说太抽象,还是来点实例痛快
import java.util.Date;
/**
* Always remember that your instance variables will be either mutable or immutable.
* Identify them and return new objects with copied content for all mutable objects.
* Immutable variables can be returned safely without extra effort.
* */
public final class ImmutableClass
{
/**
* Integer class is immutable as it does not provide any setter to change its content
* */
private final Integer immutableField1;
/**
* String class is immutable as it also does not provide setter to change its content
* */
private final String immutableField2;
/**
* Date class is mutable as it provide setters to change various date/time parts
* */
private final Date mutableField;
//Default private constructor will ensure no unplanned construction of class
private ImmutableClass(Integer fld1, String fld2, Date date)
{
this.immutableField1 = fld1;
this.immutableField2 = fld2;
this.mutableField = new Date(date.getTime());
}
//Factory method to store object creation logic in single place
public static ImmutableClass createNewInstance(Integer fld1, String fld2, Date date)
{
return new ImmutableClass(fld1, fld2, date);
}
//Provide no setter methods
/**
* Integer class is immutable so we can return the instance variable as it is
* */
public Integer getImmutableField1() {
return immutableField1;
}
/**
* String class is also immutable so we can return the instance variable as it is
* */
public String getImmutableField2() {
return immutableField2;
}
/**
* Date class is mutable so we need a little care here.
* We should not return the reference of original instance variable.
* Instead a new Date object, with content copied to it, should be returned.
* */
public Date getMutableField() {
return new Date(mutableField.getTime());
}
@Override
public String toString() {
return immutableField1 +" - "+ immutableField2 +" - "+ mutableField;
}
}
验证以上不可变类:
import java.util.Date;
public class MainTest
{
public static void main(String[] args)
{
ImmutableClass im = ImmutableClass.createNewInstance(100,"test", new Date());
System.out.println(im);
tryModification(im.getImmutableField1(),im.getImmutableField2(),im.getMutableField());
System.out.println(im);
}
private static void tryModification(Integer immutableField1, String immutableField2, Date mutableField)
{
immutableField1 = 10000;
immutableField2 = "test changed";
mutableField.setDate(10);
}
}
输出结果如下:
100 - test - Tue Jun 09 23:14:01 CST 2015
100 - test - Tue Jun 09 23:14:01 CST 2015
从输出中可以看出:即使通过对象引用改变对象变量,值依然不改变,因此类是不可变类
参考这里:click here
涉及资料:click here
官方教程: click here