是否可以使对象“只读”。一种方法 [英] Is it possible to make an object "Read Only" to a method

查看:119
本文介绍了是否可以使对象“只读”。一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将对象引用传递给方法,是否可以将对象设置为Read Only?

解决方案

严格来说。也就是说,可以改变对象的引用不能转换为不能改变对象的引用。此外,除了使用约定之外,没有办法表明类型是不可变的或可变的。



确保某种形式的不变性的唯一特征是 final 字段 - 一旦编写就无法修改。



也就是说,有方法可以设计类以便防止意外突变。以下是一些技巧:




  • 防御性复制 。传递一个对象的副本,这样如果它被突变,它不会破坏你的内部不变量。


  • 使用访问修饰符和/或接口仅公开只读方法。您可以使用访问修改( public / private / protected ),可能与界面结合,以便只有某些方法对另一个对象可见。如果公开的方法本质上是只读的,那么您就是安全的。


  • 默认情况下使对象不可变。对象的任何操作实际上都返回对象的副本。




另外,请注意SDK中的API有有时返回对象的不可变版本的方法,例如 Collections.unmodifiableList 。尝试改变不可变列表将引发异常。这不会静态地强制执行不变性(在编译时使用静态类型系统),但是它是一种廉价而有效的方法来动态地(在运行时)强制执行它。



<有许多关于Java扩展的研究提案,以更好地控制别名和可访问性。例如,添加 readonly 关键字。据我所知,它们都没有包含在未来的Java版本中。如果您有兴趣,可以查看这些指示:





Checker Framework非常有趣。在Checker Framework中,查看Generic Universe Types checker,IGJ immutability checker和Javari immutability checker。该框架使用注释工作,因此它不具有侵入性。


If an object reference is passed to a method, is it possible to make the object "Read Only" to the method?

解决方案

Not strictly speaking. That is, a reference that can mutate an object can not be turned into a reference that can not mutate an object. Also, there is not way to express that a type is immutable or mutable, other than using conventions.

The only feature that ensure some form of immutability would be final fields - once written they can not be modified.

That said, there are ways to design classes so that unwanted mutation are prevented. Here are some techniques:

  • Defensive Copying. Pass a copy of the object, so that if it is mutated it doesn't break your internal invariants.

  • Use access modifiers and/or interface to expose only read-only methods. You can use access modifieres (public/private/protected), possibly combined with interface, so that only certain methods are visible to the other object. If the methods that are exposed are read-only by nature, you are safe.

  • Make your object immutable by default. Any operation on the object returns actually a copy of the object.

Also, note that the API in the SDK have sometimes methods that return an immutable version of an object, e.g. Collections.unmodifiableList. An attempt to mutate an immutable list will throw an exception. This does not enforce immutability statically (at compile-time with the static type system), but is is a cheap and effective way to enforce it dynamically (at run-time).

There has been many research proposals of Java extension to better control of aliasing, and accessibility. For instance, addition of a readonly keyword. None of them is as far as I know planned for inclusion in future version of Java. You can have a look at these pointers if you're interested:

The Checker Framework is very interesting. In the Checker Framework, look at Generic Universe Types checker, IGJ immutability checker, and Javari immutability checker. The framework works using annotations, so it is not intrusive.

这篇关于是否可以使对象“只读”。一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆