what is “failure atomicity” used by J bloch and how its beneficial in terms of immutable object?

非 Y 不嫁゛ 提交于 2019-11-30 03:38:35

Bloch's "Failure atomicity" means that if a method threw an exception, the object should still be usable afterwards. Generally, the object should be in the same state as it was before invoking the method.

In the case of an immutable object, you gain that simply from the fact that it's immutable. There is no operation that changes the object's state. All the methods of the object may do is create new objects that are derived from the original object.

For example, String has a substring(int) method. It does not change anything in the original string - it creates a new object whose content is a copy of the part of the original string that you wanted. If it throws an exception, then you simply won't get the new object - but the original String was never changed. There is no code inside substring() that modifies the original String and therefore, it is failure-atomic.

Failure atomicity can be gained for mutable objects as well, but then you have to pay special attention to it, whereas in immutable objects, it simply follows from the care you took to design it to be immutable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!