A Strategy for Defining Immutable Objects

The following rules define a simple strategy for creating immutable objects. Not all classes documented as "immutable" follow these rules. This does not necessarily mean the creators of these classes were sloppy — they may have good reason for believing that instances of their classes never change after construction. However, such strategies require sophisticated analysis and are not for beginners.

  1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
  2. Make all fields final and private.
  3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
  4. If the instance fields include references to mutable objects, don't allow those objects to be changed:
    • Don't provide methods that modify the mutable objects.
    • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

Applying this strategy to SynchronizedRGB results in the following steps:

  1. There are two setter methods in this class. The first one, set, arbitrarily transforms the object, and has no place in an immutable version of the class. The second one, invert, can be adapted by having it create a new object instead of modifying the existing one.
  2. All fields are already private; they are further qualified as final.
  3. The class itself is declared final.
  4. Only one field refers to an object, and that object is itself immutable. Therefore, no safeguards against changing the state of "contained" mutable objects are necessary.

After these changes, we have ImmutableRGB:

final public class ImmutableRGB {

    // Values must be between 0 and 255.
final private int red;
final private int green;
final private int blue;
final private String name; private void check(int red,
int green,
int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
} public ImmutableRGB(int red,
int green,
int blue,
String name) {
check(red, green, blue);
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
} public int getRGB() {
return ((red << 16) | (green << 8) | blue);
} public String getName() {
return name;
} public ImmutableRGB invert() {
return new ImmutableRGB(255 - red,
255 - green,
255 - blue,
"Inverse of " + name);
}
}

译文:
固定对象的定义方法
  下面的一些规则描述了固定对象定义的方法。并不是所有的"固定的"类都需要遵从这些规则。这并不意味着创建着是马虎的——他们有理由相信被构造的固定类永远不会被改变。然而,这种策略需要复杂的分析,不适合初学者。
  1。不要提供“setter”方法-改变字段或对象的域名。
  2。让所有的域都是固定的或者私有的。 
  3。不要让子类覆盖父方法,做到这一点最简单的方法是用final.更复杂的方法是私有化构造函数,用工厂实例。
  4。如果实例对象包括对非固定对象的引用,这些对象不允许改变。
    不要提供改变可变对象的方法。
    不要共享对可变对象的引用。决不保存通过构造函数传递的外部,可变对象的引用。如果必要,可以创建副本,副本的存储引用。同样,创建你内部使用的可变对象的副本,而不是直接使用原来的。
在SynchronizedRGB 中用上边的规则的使用步骤:
  1。在这个类中有两个"setter"方法。第一个,设置,任意变换对象,并没有在一个固定类的地方。第二个,反之,可适应它创建一个新的对象而不是修改现有的。
  2。所有对象都是私有的,更进异步成为固定的。
  3。这个类本身被申明为固定的。
  4。只有一个字段,指的是一个对象,该对象本身是不可变的。因此,没有保证反对改变可变对象包含的状态是必须的。
在这些改变之后,我们有 ImmutableRGB:
 final public class ImmutableRGB {

     // Values must be between 0 and 255.
final private int red;
final private int green;
final private int blue;
final private String name; private void check(int red,
int green,
int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
} public ImmutableRGB(int red,
int green,
int blue,
String name) {
check(red, green, blue);
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
} public int getRGB() {
return ((red << 16) | (green << 8) | blue);
} public String getName() {
return name;
} public ImmutableRGB invert() {
return new ImmutableRGB(255 - red,
255 - green,
255 - blue,
"Inverse of " + name);
}
}
 
 

【翻译十六】java-固定对象的定义方法的更多相关文章

  1. 菜鸡的Java笔记 第二十四 - java 接口的基本定义

    1.接口的基本定义以及使用形式        2.与接口有关的设计模式的初步认识        3.接口与抽象类的区别                 接口与抽象类相比,接口的使用几率是最高的,所有的 ...

  2. 菜鸡的Java笔记 第二十六 - java 内部类

    /*    innerClass        从实际的开发来看,真正写到内部类的时候是在很久以后了,短期内如果是自己编写代码,几乎是见不到内部类出现的        讲解它的目的第一个是为了解释概念 ...

  3. Eclipse用法和技巧十六:自动添加未实现方法2

    前面一篇文章里面介绍了一种常见的自动添加未实现函数的方法.这里在顺便补充几个方法.第一个方法,看上去有点怪怪的:        步骤一:Source > Clean Up: 步骤二:选择cust ...

  4. JAVA学习第四十六课 — 其它对象API(二)Date类 &amp; Calendar类(重点掌握)

    Date类(重点) 开发时,会时常遇见时间显示的情况,所以必须熟练Date的应用 <span style="font-family:KaiTi_GB2312;font-size:18p ...

  5. Java 读书笔记 (十六) Java 继承

    例: 开发动物类,其中动物分别为企鹅以及老鼠,要求如下: 企鹅: 属性(姓名,id), 方法(吃,睡,自我介绍) 老鼠: 属性(姓名,id), 方法(吃,睡,自我介绍) 企鹅类: public cla ...

  6. Java学习笔记十六:Java中的构造方法

    Java中的构造方法 1.使用new+构造方法 创建一个新的对象: 2.构造方法是定义在Java类中的一个用来初始化对象的方法: 3.构造方法与类同名且没有返回值: 4.语法格式: public 构造 ...

  7. 菜鸡的Java笔记 第三十六 - java 函数式编程

    StudyLambda    Lambda 指的是函数式编程,现在最为流行的编程模式为面向对象,很多的开发者并不认可面向对象,所以很多的开发者宁愿继续使用 C 语言进行开发,也不愿意使用java,c+ ...

  8. 菜鸡的Java笔记 第十六 - java 引用传递

    referenceDelivery    引用传递是整个java 的精髓,也是所有初学者最难学的地方        引用的本质:同一块堆内存可以被不同的栈内存所指向    下面通过三道程序来进行引用传 ...

  9. Java从零开始学三十六(JAVA IO- 字符流)

    一.字符流 BufferedReader:BufferedReader是从缓冲区之中读取内容,所有的输入的字节数据都将放在缓冲区之中 BufferedWriter:把一批数据写入到缓冲区,当缓冲区区的 ...

随机推荐

  1. respberry pi3 上手随记

    入门篇: raspberry 官网: https://www.raspberrypi.org/ 各个系统镜像下载地址: https://www.raspberrypi.org/downloads/ 树 ...

  2. Win7 & VS2013 编译 WebKit 总结

      [转载请注明Moon出品: http://blog.csdn.net/lzl124631x] 距离上次编译WebKit(实际日期是2012年10月)已经过去一年多了, 这期间有很多人问我编译相关的 ...

  3. ssh免密码登入

    通常做许多事情(git puh/脚本等等),不停输入密码是件很不愉快的事情,破解如下: http://www.linuxproblem.org/art_9.html 1. 生成rsa密钥 ssh-ke ...

  4. FREE 开源 API 管理工具等

    最近学习API 管理工具,发现几个不错的东西,记录如下: 1.IBM 收购NODE 厂家  STRONGLOOP 有一产品LOOPBACK,开源,好! 2.apigee  api管理平台 也不错. 3 ...

  5. Selenium2(webdirver)入门之环境搭建(Java版)

    一.开发环境: 1.JDK1.6 2.Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/ ...

  6. CentOS 7部署flume

    CentOS 7部署flume 准备工作: 安装java并设置java环境变量,在`/etc/profile`中加入 export JAVA_HOME=/usr/java/jdk1.8.0_65 ex ...

  7. redis pub/sub 实战: 微信语音识别

    2015年5月22日 20:20:20 星期五 效果: 这边对微信说话,  浏览器端及时显示语音识别的文字 注意: 在连接socket.io时, 按下浏览器f12, 如果一直有请求不断的刷, 说明so ...

  8. selenium 配合sikuli script操作高德地图

    会不会使用工具,是一般QA和高级QA的区别 ---To be crazy Java就是好,开源框架遍地都是,各种niubility的jar包,各种神器,真是不亦乐乎. 今天研究一下基于图片识别作为对象 ...

  9. Divide and conquer:Subset(POJ 3977)

    子序列 题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数 题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通 ...

  10. 【编程题目】栈的 push、pop 序列

    29.栈的 push.pop 序列(栈)题目:输入两个整数序列.其中一个序列表示栈的 push 顺序,判断另一个序列有没有可能是对应的 pop 顺序.为了简单起见,我们假设 push 序列的任意两个整 ...