不可变对象是指一个对象的状态在对象被创建之后就不再变化。
不可变对象对于缓存是非常好的选择,因为你不需要担心它的值会被更改。
创建一个不可变类:
-
将类声明为final,所以它不能被继承;
-
将所有的成员声明为私有的,这样就不允许直接访问这些成员;
-
对变量不要提供setter方法;
-
将所有可变的成员声明为final,这样只能对它们赋值一次;
-
通过构造器初始化所有成员,进行深拷贝(deep copy);
-
在getter方法中,不要直接返回对象本身,而是克隆对象,并返回对象的拷贝;
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public
final
class
Immutable {
private
final
int
id;
private
final
String name;
private
final
HashMap map;
public
int
getId() {
return
id;
}
public
String getName() {
return
name;
}
/**
* 可变对象的访问方法
*/
public
HashMap getMap() {
return
(HashMap) testMap.clone();
}
/**
* 实现深拷贝的构造器*/
public
Immutable(
int
i, String n, HashMap hm){
this
.id=i;
this
.name=n;
HashMap tempMap=
new
HashMap();
String key;
Iterator it = hm.keySet().iterator();
while
(it.hasNext()){
key=it.next();
tempMap.put(key, hm.get(key));
}
this
.map = tempMap;
}
}
|