【翻译十六】java-固定对象的定义方法
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.
- Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
- Make all fields
final
andprivate
. - 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 constructorprivate
and construct instances in factory methods. - 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:
- 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. - All fields are already
private
; they are further qualified asfinal
. - The class itself is declared
final
. - 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);
}
}
译文:
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-固定对象的定义方法的更多相关文章
- 菜鸡的Java笔记 第二十四 - java 接口的基本定义
1.接口的基本定义以及使用形式 2.与接口有关的设计模式的初步认识 3.接口与抽象类的区别 接口与抽象类相比,接口的使用几率是最高的,所有的 ...
- 菜鸡的Java笔记 第二十六 - java 内部类
/* innerClass 从实际的开发来看,真正写到内部类的时候是在很久以后了,短期内如果是自己编写代码,几乎是见不到内部类出现的 讲解它的目的第一个是为了解释概念 ...
- Eclipse用法和技巧十六:自动添加未实现方法2
前面一篇文章里面介绍了一种常见的自动添加未实现函数的方法.这里在顺便补充几个方法.第一个方法,看上去有点怪怪的: 步骤一:Source > Clean Up: 步骤二:选择cust ...
- JAVA学习第四十六课 — 其它对象API(二)Date类 & Calendar类(重点掌握)
Date类(重点) 开发时,会时常遇见时间显示的情况,所以必须熟练Date的应用 <span style="font-family:KaiTi_GB2312;font-size:18p ...
- Java 读书笔记 (十六) Java 继承
例: 开发动物类,其中动物分别为企鹅以及老鼠,要求如下: 企鹅: 属性(姓名,id), 方法(吃,睡,自我介绍) 老鼠: 属性(姓名,id), 方法(吃,睡,自我介绍) 企鹅类: public cla ...
- Java学习笔记十六:Java中的构造方法
Java中的构造方法 1.使用new+构造方法 创建一个新的对象: 2.构造方法是定义在Java类中的一个用来初始化对象的方法: 3.构造方法与类同名且没有返回值: 4.语法格式: public 构造 ...
- 菜鸡的Java笔记 第三十六 - java 函数式编程
StudyLambda Lambda 指的是函数式编程,现在最为流行的编程模式为面向对象,很多的开发者并不认可面向对象,所以很多的开发者宁愿继续使用 C 语言进行开发,也不愿意使用java,c+ ...
- 菜鸡的Java笔记 第十六 - java 引用传递
referenceDelivery 引用传递是整个java 的精髓,也是所有初学者最难学的地方 引用的本质:同一块堆内存可以被不同的栈内存所指向 下面通过三道程序来进行引用传 ...
- Java从零开始学三十六(JAVA IO- 字符流)
一.字符流 BufferedReader:BufferedReader是从缓冲区之中读取内容,所有的输入的字节数据都将放在缓冲区之中 BufferedWriter:把一批数据写入到缓冲区,当缓冲区区的 ...
随机推荐
- maven项目部分知识
1.maven项目在pom.xml中用add dependencies加入jar包,搜索不到jar包的解决方案: Window --> Show View --> Other输入mave ...
- 51Nod 1378 夹克老爷的愤怒
Description 一棵树,可以进行染色,被染色的点可以控制与它距离不超过 \(k\) 的所有点,问控制整棵树最少需要染几个点. Sol 贪心. 记录一下最深的未染色点和最浅的染色点,判断一下能否 ...
- 使用PopupWindow
PopupWindow可以用来实现弹出任意位置的菜单,比Context Menu和Option Menu灵活性更高.Android中弹出一个PopupWindow基本有两个方法: 1 2 //Disp ...
- Quartz表达式详解(转载)
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...
- 【GoLang】GoLang for 中有多个循环变量怎么处理?
代码示例: sum := , ; i <= && j <= ; i, j = i+, j- { t.Log("i: ", i) t.Log(" ...
- SqlServer中的Null值空值问题
sql使用的是三值谓词逻辑,所以逻辑表达式返回的结果可以为True.False或者未知,在三值逻辑中返回True与不返回False并不完全一样, SQL对查询过滤条件的处理:接受TURE 拒绝FAL ...
- Node.js——Async
一:流程控制 为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程 ...
- SQL查询表中的有那些索引
方法1. 使用系统表 -- 查询一个表中的索引及索引列 USE AdventureWorks2008 GO SELECT indexname = a.name , tablename = c. n ...
- percona-toolkit 之 【pt-slave-delay】说明
摘要: 在主从复制的架构中,正常情况下主上操作的记录也会在从上进行操作,虽说是异步复制,但操作会“实时”的同步到从.比如在主上不小心误操作了,还没等反应过来从上也会马上执行误操作,后期只有通过二进制或 ...
- MySQL 通过idb文件恢复Innodb 数据【转】
昨晚收到一则求助,一个用户的本地数据库的重要数据由于误操作被删除,需要进行紧急恢复,用户的数据库日常并没有进行过任何备份,binlog也没有开启,所以从备份和binlog入手已经成为不可能,咨询了丁奇 ...