当冬日阳光照耀我孤苦的心怀
2010-11-14 14:51
246 查看
Java中很多class都是immutable,像String,Integer等,它们通常用来作为Map的key.
那么在实现自定义的Immutable的Class的时候,应该注意哪些要点呢?
a)Class 应该定义成final,避免被继承。
b)所有的成员变量应该被定义成final。
c)不要提供可以改变类状态(成员变量)的方法。【get 方法不要把类里的成员变量让外部客服端引用,当需要访问
成员变量时,换回成员变量的copy】
d)构造函数不要引用外部可变对象。如果需要引用外部可以变量,应该在构造函数里进行defensive copy。
Wrong way to write a constructor:
public final class MyImmutable {
private final int[] myArray;
public MyImmutable(int[] anArray) {
this.myArray = anArray; // wrong
}
public String toString() {
StringBuffer sb = new StringBuffer("Numbers are: ");
for (int i = 0; i < myArray.length; i++) {
sb.append(myArray[i] + " ");
}
return sb.toString();
}
}
// the caller could change the array after calling the
constructor.
int[] array = {1,2};
MyImmutable myImmutableRef = new MyImmutable(array) ;
System.out.println("Before constructing " + myImmutableRef);
array[1] = 5; // change (i.e. mutate) the element
System.out.println("After constructing " + myImmutableRef);
Out put:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 5
Right way to write an immutable class
Right way is to copy the array before assigning in the constructor.
public final class MyImmutable {
private final int[] myArray;
public MyImmutable(int[] anArray) {
this.myArray = anArray.clone(); // defensive copy
}
public String toString() {
StringBuffer sb = new StringBuffer("Numbers are: ");
for (int i = 0; i < myArray.length; i++) {
sb.append(myArray[i] + " ");
}
return sb.toString();
}
}
// the caller cannot change the array after calling the constructor.
int[] array = {1,2};
MyImmutable myImmutableRef = new MyImmutable(array) ;
System.out.println("Before constructing " + myImmutableRef);
array[1] = 5; // change (i.e. mutate) the element
System.out.println("After constructing " + myImmutableRef);
Out put:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 2
那么在实现自定义的Immutable的Class的时候,应该注意哪些要点呢?
a)Class 应该定义成final,避免被继承。
b)所有的成员变量应该被定义成final。
c)不要提供可以改变类状态(成员变量)的方法。【get 方法不要把类里的成员变量让外部客服端引用,当需要访问
成员变量时,换回成员变量的copy】
d)构造函数不要引用外部可变对象。如果需要引用外部可以变量,应该在构造函数里进行defensive copy。
Wrong way to write a constructor:
public final class MyImmutable {
private final int[] myArray;
public MyImmutable(int[] anArray) {
this.myArray = anArray; // wrong
}
public String toString() {
StringBuffer sb = new StringBuffer("Numbers are: ");
for (int i = 0; i < myArray.length; i++) {
sb.append(myArray[i] + " ");
}
return sb.toString();
}
}
// the caller could change the array after calling the
constructor.
int[] array = {1,2};
MyImmutable myImmutableRef = new MyImmutable(array) ;
System.out.println("Before constructing " + myImmutableRef);
array[1] = 5; // change (i.e. mutate) the element
System.out.println("After constructing " + myImmutableRef);
Out put:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 5
Right way to write an immutable class
Right way is to copy the array before assigning in the constructor.
public final class MyImmutable {
private final int[] myArray;
public MyImmutable(int[] anArray) {
this.myArray = anArray.clone(); // defensive copy
}
public String toString() {
StringBuffer sb = new StringBuffer("Numbers are: ");
for (int i = 0; i < myArray.length; i++) {
sb.append(myArray[i] + " ");
}
return sb.toString();
}
}
// the caller cannot change the array after calling the constructor.
int[] array = {1,2};
MyImmutable myImmutableRef = new MyImmutable(array) ;
System.out.println("Before constructing " + myImmutableRef);
array[1] = 5; // change (i.e. mutate) the element
System.out.println("After constructing " + myImmutableRef);
Out put:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 2
相关文章推荐
- 冬日的阳光
- 享受冬日阳光的温暖
- 总有云开日出时候, 万丈阳光照耀你我
- 心存希望,幸福就会靠近你;心存梦想,机遇就会笼罩你;心存坚持,快乐就会常伴你;心存善念,阳光就会照耀你;心存美丽,温暖就会围绕你;心存他人,真情就会回报你;心存感恩,贵人就会青睐你。
- 寒冬日,找阳光
- 寒冬日,找阳光
- 迎向冬日的阳光
- 冬日阳光
- 冬日里,28℃的爱情
- 阳光行为,阳光心情
- 你想买空气,水还是阳光?
- 阳光明媚的一天~|~
- 塑造阳光心态
- 机器狗病毒检测工具——阳光网络开发20071025(随新变种即时更新)
- 一米阳光。
- 银光闪现!Silverlight照耀微软未来
- 我看到东边的阳光就这样照进车窗
- 阳光已经好久不见
- 美--寻找属于自己的阳光
- 所谓英雄,其实是一群心怀畏惧的人