- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
ImmutableSet实现 Set界面。对 ImmutableSet
没有意义的函数现在称为 Set
的“可选操作”。我假设是这样的情况。所以 ImmutableSet
现在会为许多可选操作抛出 UnsupportedOperationException
。
这对我来说似乎倒退了。有人告诉我,接口(interface)是一种契约,因此您可以在不同的实现中使用强加功能。可选操作的方法似乎从根本上改变了(矛盾?)接口(interface)的用途。今天要实现这个,我会将 Set
接口(interface)分成两个接口(interface):一个用于不可变操作,另一个用于扩展这些操作以用于修改器。 (非常快,现成的解决方案)
我了解技术在不断变化。我不是说它应该以某种方式完成。我的问题是,这种变化是否反射(reflect)了 Java 某些基本理念的变化?使事情向后兼容只是一个创可贴吗?我对接口(interface)的理解不完整吗?
最佳答案
Java Collections API Design FAQ详细回答这个问题:
Q: Why don't you support immutability directly in the core collection interfaces so that you can do away with optional operations (and UnsupportedOperationException)?
A: This is the most controversial design decision in the whole API. Clearly, static (compile time) type checking is highly desirable, and is the norm in Java. We would have supported it if we believed it were feasible. Unfortunately, attempts to achieve this goal cause an explosion in the size of the interface hierarchy, and do not succeed in eliminating the need for runtime exceptions (though they reduce it substantially).
Doug Lea, who wrote a popular Java collections package that did reflect mutability distinctions in its interface hierarchy, no longer believes it is a viable approach, based on user experience with his collections package. In his words (from personal correspondence) "Much as it pains me to say it, strong static typing does not work for collection interfaces in Java."
To illustrate the problem in gory detail, suppose you want to add the notion of modifiability to the Hierarchy. You need four new interfaces: ModifiableCollection, ModifiableSet, ModifiableList, and ModifiableMap. What was previously a simple hierarchy is now a messy heterarchy. Also, you need a new Iterator interface for use with unmodifiable Collections, that does not contain the remove operation. Now can you do away with UnsupportedOperationException? Unfortunately not.
Consider arrays. They implement most of the List operations, but not remove and add. They are "fixed-size" Lists. If you want to capture this notion in the hierarchy, you have to add two new interfaces: VariableSizeList and VariableSizeMap. You don't have to add VariableSizeCollection and VariableSizeSet, because they'd be identical to ModifiableCollection and ModifiableSet, but you might choose to add them anyway for consistency's sake. Also, you need a new variety of ListIterator that doesn't support the add and remove operations, to go along with unmodifiable List. Now we're up to ten or twelve interfaces, plus two new Iterator interfaces, instead of our original four. Are we done? No.
Consider logs (such as error logs, audit logs and journals for recoverable data objects). They are natural append-only sequences, that support all of the List operations except for remove and set (replace). They require a new core interface, and a new iterator.
And what about immutable Collections, as opposed to unmodifiable ones? (i.e., Collections that cannot be changed by the client AND will never change for any other reason). Many argue that this is the most important distinction of all, because it allows multiple threads to access a collection concurrently without the need for synchronization. Adding this support to the type hierarchy requires four more interfaces.
Now we're up to twenty or so interfaces and five iterators, and it's almost certain that there are still collections arising in practice that don't fit cleanly into any of the interfaces. For example, the collection-views returned by Map are natural delete-only collections. Also, there are collections that will reject certain elements on the basis of their value, so we still haven't done away with runtime exceptions.
When all was said and done, we felt that it was a sound engineering compromise to sidestep the whole issue by providing a very small set of core interfaces that can throw a runtime exception.
简而言之,拥有像 Set
这样带有可选操作的接口(interface)是为了防止所需的不同接口(interface)数量呈指数级增长。它不仅仅是“不可变”和“可变”那么简单。 Guava 的 ImmutableSet
然后必须实现 Set
才能与所有其他使用 Set
的代码互操作。这并不理想,但确实没有更好的方法。
关于java - 为什么他们决定让接口(interface)有 "Optional Operations",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657191/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!