Scala 集合转发器和代理的用例 [英] Use cases of Scala collection forwarders and proxies

查看:42
本文介绍了Scala 集合转发器和代理的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala 的集合库包含转发器 IterableForwarder, TraversableForwarder, SeqForwarderIterableProxy 等代理MapProxySeqProxy, SetProxy,TraversableProxy 等转发器和代理都将集合方法委托给底层集合对象.这两者之间的主要区别在于转发器不会转发会创建相同类型的新集合对象的调用.

Scala's collection library contains the forwarders IterableForwarder, TraversableForwarder, SeqForwarder and proxies like IterableProxy, MapProxy, SeqProxy, SetProxy, TraversableProxy, etc. Forwarders and proxies both delegate collection methods to an underlying collection object. The main difference between these two are that forwarders don't forward calls that would create new collection objects of the same kind.

在哪些情况下,我更喜欢这些类型中的一种?货运代理为何以及何时有用?如果它们有用,为什么没有 MapForwarderSetForwarder ?

In which cases would I prefer one of these types over the other? Why and when are forwarders useful? And if they are useful why are there no MapForwarder and SetForwarder?

如果您想使用其他方法为集合构建包装器或对标准集合进行拉皮条,我认为最常使用代理.

I assume proxies are most often used if one wants to build a wrapper for a collection with additional methods or to pimp the standard collections.

推荐答案

我认为 这个答案 提供了一些关于 Proxy 的一般情况(你对包装和拉皮条的假设是正确的).

I think this answer provides some context about Proxy in general (and your assumption about wrapper and pimping would be correct).

据我所知,Proxy 的子类型更针对最终用户.当使用 Proxy 时,代理对象和 self 对象对于所有意图和目的来说都是相等.我认为这实际上是主要区别.如果该假设不成立,请不要使用 Proxy.

As far as I can tell the subtypes of Proxy are more targeted to end users. When using Proxy the proxy object and the self object will be equal for all intent and purposes. I think that's actually the main difference. Don't use Proxy if that assumption does not hold.

Forwarder 特性似乎仅用于支持 ListBuffer,如果需要推出基于 的自己的集合类,则可能更合适>CanBuildFrom 基础设施.所以我会说它更适合图书馆作家,因为图书馆基于 2.8 馆藏设计.

The Forwarder traits only seems to be used to support ListBuffer and may be more appropriate if one needs to roll out their own collection class built on top of the CanBuildFrom infrastructure. So I would say it's more targeted to library writers where the library is based on the 2.8 collection design.

这篇关于Scala 集合转发器和代理的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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