A Strategy for Defining Immutable Objects
来源:互联网 发布:软件项目管理期末试卷 编辑:程序博客网 时间:2024/06/03 19:46
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); }}
0 0
- A Strategy for Defining Immutable Objects
- The Java™ Tutorials — Concurrency :A Strategy for Defining Immutable Objects 一个定义不可变对象的策略
- Defining Classes and Objects
- Defining a Copy Constructor for a Reference Class Type
- Mutable and Immutable Objects
- Immutable Objects 不可变对象
- 不变对象(Immutable Objects)
- Notes for Python's function# Defining a class class class_name: [statement 1] [statement 2]
- Java不可变对象(Immutable Objects)
- Defining a Business Add-In
- Defining a Custom Input Source
- Defining a controller with @Controller
- 5.6.3.1 Defining a Frustum
- (Item 4) Avoid creating duplicate objects, reuse immutable objects
- iOS 多层级的immutable objects 转换成 mutable objects
- iOS 多层级的immutable objects 转换成 mutable objects
- My top ten list for a successful test automation strategy
- WWDC2014 Session 237 A Strategy For Great Work
- The last packet successfully received from the server was 78,682,686 milliseconds ago
- Exercise 37: 复习各种符号
- 小米随身Wi-Fi 已于7 日通过NCC 认证会上市
- SQL 函数
- DevExpress之TreeList的研究绑定XML文件
- A Strategy for Defining Immutable Objects
- 点击事件的4种写法
- zoj3533 Gao the String I(splay)
- HTTP协议详解
- java 泛型小知识
- PAT (Basic Level) Practise (中文)1004. 成绩排名 (20)
- Burp suite 暴力破解shell密码详细教程
- do...while(0)的妙用
- linux下安装mysql出现错误conflicts with file from package