The Java™ Tutorials — Concurrency :A Strategy for Defining Immutable Objects 一个定义不可变对象的策略

The Java™ Tutorials — Concurrency :A Strategy for Defining Immutable Objects 一个定义不可变对象的策略

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

关键点

一个定义不可变对象的策略:

  1. 不要提供setter方法,也就是指那些可以改变字段或字段所引用对象的方法。
  2. 把所有字段标记为final和private
  3. 不要允许子类覆写这些方法。最简单的方式就是讲类声明为final。一个更复杂的方式是,让构造器私有,并利用工厂方法创建实例
  4. 如果实例字段包含了对可变对象的引用,不要允许对这些对象的改变: 
    • 不要提供可改变这些可变对象的方法
    • 不要共享这些对可变对象的引用。坚决不要将这些引用存储到类外,也不要存储到由构造器传入的变量中;如有必要,创建副本,并将引用存储到副本之中。同样的,必要情况下,创建你的内部可变变量的引用副本, 以避免返回你方法中原来的对象。

全文翻译

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.

下面的规则定义了一种创建不可变对象的简单策略。并非所有的被标记为“不可变”的类都需要遵守此规则。这并非一定意味着这些类的创建者都是马马虎虎的——他们也许有很好的理由相信他们类的实例在构造出后永远不会被改动。然而,这样的策略需要复杂的分析,这并不适用于初学者。

  1. 不要提供setter方法,也就是指那些可以改变字段或字段所引用对象的方法。
  2. 把所有字段标记为final和private
  3. 不要允许子类覆写这些方法。最简单的方式就是讲类声明为final。一个更复杂的方式是,让构造器私有,并利用工厂方法创建实例
  4. 如果实例字段包含了对可变对象的引用,不要允许对这些对象的改变: 
    • 不要提供可改变这些可变对象的方法
    • 不要共享这些对可变对象的引用。坚决不要将这些引用存储到类外,也不要存储到由构造器传入的变量中;如有必要,创建副本,并将引用存储到副本之中。同样的,必要情况下,创建你的内部可变变量的引用副本, 以避免返回你方法中原来的对象。

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.

将此策略应用到SynchronizedRGB中,你就需要做如下步骤:

  1. 在类中有两个Setter方法。第一个,也就是set方法,会随意地转换对象,它不应该在此不可变版本中存在。第二个,invert方法,可以用创建新对象的方法而非修改现存对象的方式修改。
  2. 所有的字段都已经为private,它们需要进一步处理为final
  3. 把这个类本身定义为final
  4. 只有一个字段引用了一个对象,而这个对象本身就是不可变对象。因此,也就无需那些防止内内置可变对象被修改的策略了。

After these changes, we have ImmutableRGB:

做了这些更改后,我们便有了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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值