`
leonzhx
  • 浏览: 791639 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1.  The Java platform libraries contain many immutable classes, including String, the boxed primitive classes, and BigInteger and BigDecimal.

 

2.  Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.

 

3.  To make a class immutable:
    a)  Don’t provide any methods that modify the object’s state (known as mutators).
    b)  Ensure that the class can’t be extended.
    c)  Make all fields final.
    d)  Make all fields private.
    e)  Ensure exclusive access to any mutable components. Make defensive copies in constructors, accessors, and readObject methods for those fields that refer to mutable objects.

Commented By Sean: Why readObject ? If all others accesses are exclusive , is it possible to have multiple reference to the same mutable components serialized in the stream?

 

4.  Immutable objects are simple. An immutable object can be in exactly one state, the state in which it was created.


5.  Immutable objects are inherently thread-safe; they require no synchronization and can be shared freely.

 

6.  An immutable class can provide static factories that cache frequently requested instances to avoid creating new instances when existing ones would do. All the boxed primitive classes and BigInteger do this.

 

7.  Opting for static factories in place of public constructors when designing a new class gives you the flexibility to add caching later, without modifying clients.

 

8.  You never have to make defensive copies. Therefore, you need not and should not provide a clone method or copy constructor on an immutable class.

 

9.  Not only can you share immutable objects, but you can share their internals. i.e. BigInteger.negate shares its internal magnitude representation ( int array) and String.substring shares its internal char array.

 

10.  Immutable objects make great building blocks for other objects. It’s much easier to maintain the invariants of a complex object if you know that its component objects will not change underneath it. (i.e. map keys and set elements).


11.  The only real disadvantage of immutable classes is that they require a separate object for each distinct value. Creating these objects can be costly, especially if they are large.

 

12.  The performance problem is magnified if you perform a multistep operation that generates a new object at every step, eventually discarding all objects except the final result. The approach is to guess which multistep operations will be commonly required and provide them as primitives. If a multistep operation is provided as a primitive, the immutable class does not have to create a separate object at each step. Or you can provide a public mutable companion class.(i.e. StringBuilder for String and BitSet for BigInteger)

 

13.  The alternative to making an immutable class final is to make all of its constructors private or package-private, and to add public static factories in place of the public constructors.

 

14.  If you write a class whose security depends on the immutability of a BigInteger or BigDecimal argument from an untrusted client, you must check to see that the argument is a “real” BigInteger or BigDecimal, rather than an instance of an untrusted subclass. If it is the latter, you must defensively copy it under the assumption that it might be mutable, because they are not final.

 

15.  In truth, no method may produce an externally visible change in the object’s state. Some immutable classes have one or more non-final fields in which they cache the results of expensive computations the first time they are needed. (i.e. the hash field of String)

 

16.  If you choose to have your immutable class implement Serializable and it contains one or more fields that refer to mutable objects, you must provide an explicit readObject or readResolve method, or use the ObjectOutputStream.writeUnshared and ObjectInputStream.readUnshared methods, even if the default serialized form is acceptable. Otherwise an attacker could create a mutable instance of your not quite-immutable class.

 

17.  In summary, classes should be immutable unless there’s a very good reason to make them mutable. Immutable classes provide many advantages, and their only disadvantage is the potential for performance problems under certain circumstances. If a class cannot be made immutable, limit its mutability as much as possible. Make every field final unless there is a compelling reason to make it non-final.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 15: Minimize the accessibility of classes and members Item 16: In public classes, use accessor methods, not public fields Item 17: Minimize mutability Item 18: Favor composition over inheritance ...

    凸优化理论与应用-凸优化.ppt

    minimize f(x), subject to g_i(x) ≤ 0, i = 1, ..., m h_j(x) = 0, j = 1, ..., p 其中,x ∈ ℝⁿ为优化变量,f(x)为目标函数,g_i(x)和h_j(x)分别为不等式约束和等式约束。 2.局部最优解: 局部最优解是指在...

    minimize(windows窗口最小化工具)

    "minimize"是一款专为Windows操作系统设计的窗口最小化工具,它的主要功能是将打开的窗口轻松快捷地最小化到桌面,以便用户更高效、整洁地管理和浏览各个应用程序。这款工具特别适合那些需要同时处理多个任务,且...

    Effective C#

    **Item 15: Utilize using and try/finally for Resource Cleanup** - **Techniques:** - **Using Statement:** Automatically disposes of resources. - **Try/Finally Block:** Explicitly releases resources ...

    matlab开发-minimize

    在MATLAB中,`minimize`是一个非常重要的优化工具,用于寻找函数的最小值。它提供了多种算法,包括全局和局部优化方法,适用于各种不同类型的优化问题。在本项目"matlab开发-minimize"中,重点是利用MATLAB内置的`...

    Scikit-learn使用手册中文版.pdf

    minimize ||y - Xw||^2 + α||w||^2 其中,α 是惩罚参数,用于控制系数的缩减量。 1.3 Lasso Lasso 是一种预测稀疏系数的线性模型,用于选择拥有更少参数数量的模型。Lasso 的目标函数可以写成: minimize ||y ...

    另类最小化miniMIZE

    "另类最小化miniMIZE"是一个创新的浏览器插件或工具,它的主要功能是让网页以极小的尺寸在桌面上显示,这样用户就可以同时打开并查看多个网页,而无需在窗口之间频繁切换。这一特性对于那些需要同时监控多个网页内容...

    最优化理论与算法最优化理论与算法.pdf

    minimize ∑cjxj s.t. ∑aijxj ≥ bi, i = 1, ..., m xj ≥ 0, j = 1, ..., n (2)线性规划问题的 geomeric interpretation: maximize ∑cjxj s.t. ∑aijxj ≤ bi, i = 1, ..., m xj ≥ 0, j = 1, ..., n ...

    matlab精度检验代码-InvGN:计算Tikhonov正则化的高斯牛顿非线性迭代反演以解决阻尼非线性最小二乘问题(Matlab代码)

    minimize ||g(m)-d||^2_2 + lambda^2||Lm||^2_2 For appropriate choices of regularization parameter lambda, this problem is equivalent to: minimize ||Lm||_2 subject to ||g(m)-d||_2<delta (where delta ...

    机器学习课程作业11

    Minimize: f(w) Subject to: gi(w) ≤ 0, i = 1, 2, ..., k hj(w) = 0, j = 1, 2, ..., l 其中,f(w)是目标函数,gi(w) ≤ 0是约束条件,hj(w) = 0是等式约束。 对偶问题的定义如下: Maximize: L(w, a, b) ...

    wp国人双栏Minimize主题

    【标题】"wp国人双栏Minimize主题"是一款专为WordPress设计的精细化个人博客主题,以其独特的双栏布局和简洁的界面设计深受用户喜爱。这款主题不仅注重美学,更强调用户体验,旨在帮助博主们更好地展示内容,提升...

    Effective[removed]更新 EffectiveJavaScript 到 JavaScript 新版

    会以中文版为主要更新内容原版资讯Effective 后来JavaScriptChapter 1: Accustoming Yourself to JavaScript Chapter 2: Variable Scope Item 08: Minimize Use of the Global Object Item 09: Always Declare Local...

    pytorch-minimize:将scipy.optimize.minimize用作PyTorch优化器

    PyTorch最小化 的包装,使其成为PyTorch优化器,在PyTorch中实现共轭梯度,BFGS,l-BFGS,SLSQP,牛顿共轭梯度,信任区域方法等。...cd pytorch-minimize python -m pip install . 使用优化器 优化器类是

    Minimize Creator-开源

    Minimize Creator通过去除这些冗余部分,实现了文件的最小化。这个过程被称为“压缩”或“.min”化,是前端性能优化的重要环节。 1. **JavaScript压缩**:Minimize Creator可以处理.js文件,通过删除注释、多余空格...

    pytorch-minimize:使用PyTorch进行牛顿和拟牛顿优化

    Pytorch-minimize表示一组实用程序,用于最小化PyTorch中一个或多个变量的标量函数。 它受到SciPy的optimize模块和MATLAB的极大启发。 与SciPy和MATLAB不同,无需提供jacobian和hessian函数来进行pytorch最小化求解...

    《管理运筹学》考试模拟试卷(B).pdf

    - 目标函数:minimize(25x1 + 30x2 + 20x3 + 24x4) 2. **基金投资优化**: 这是一个多目标线性规划问题,目标是在保证年回报率至少6万元且风险最小的前提下分配投资。设投资A基金的单位数为x1,投资B基金的单位数...

    2004年第一届全国高校研究生数学建模竞赛B题

    minimize ∑ xij where xij is a binary variable. 同时,我们需要添加一些约束条件来描述下料问题。例如,我们可以使用以下约束条件来描述原材料的使用情况: ∑ xij ≤ 1, ∀ j 这意味着每个原材料只能被使用...

    Matlab供应与选址问题附详细编程PPT课件.pptx

    Minimize ∑∑cijXij Subject to: ∑Xij ≤ ej j=1,2 ∑Xij ≥ di i=1,…,6 Xij ≥ 0 i=1,…,6;j=1,2 非线性规划模型是指在满足一定约束条件下,使总的吨千米数最小的模型。在这个模型中,决策变量是料场的...

    非线性最小二乘lsqnonlin

    minimize ∑[f_i(x)]^2 其中,f_i(x) 是非线性函数,x 是参数向量。 LSQNONLIN 算法 LSQNONLIN 算法是 MATLAB 中的一种非线性最小二乘算法,用于解决非线性最小二乘问题。该算法基于trust-region反射算法,通过...

Global site tag (gtag.js) - Google Analytics