问题
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.
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.
回答1:
I think this answer provides some context about Proxy
in general (and your assumption about wrapper and pimping would be correct).
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.
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.
来源:https://stackoverflow.com/questions/5737534/use-cases-of-scala-collection-forwarders-and-proxies