java中接口中的方法,Java接口中的可选方法

文章讨论了在集合框架中引入可修改性、不可修改性、只读和不可变性的复杂性。原有的简单层级因为这些新需求变得混乱,导致了大量新接口和迭代器的出现。作者指出,即使如此,仍然可能存在不适合现有接口的集合类型,例如只允许追加的日志和不允许某些操作的特定集合。文章最后提到,通过提供少量核心接口并允许运行时异常,可以作为工程上的妥协来解决这个问题。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值