Java设计模式之immutable(不可变)模式
immutable简介 不可变对象永远不会发生改变,其字段的值只在构造函数运行时设置一次,其后就不会再改变 。例如JDK中常见的两种基本数据类型String和Integer,它们都是不可变对象。为了理解immutable与mutable的区别,可以看看下面的一段代码: package date0804.demo2; import java.awt.Point; public class ImmutableString { public static void main(String[] args) { //String,immutable String str = new String("new book"); System.out.println(str); str.replaceAll("new", "old"); System.out.println(str); //Point,mutable Point point = new Point(0,0); System.out.println(point); point.setLocation(1, 0); System.out.println(point); } } 运行结果为 new book new book java.awt.Point[x=0,y=0] java.awt.Point[x=1,y=0]