不可变集合Actionscript 3 [英] Immutable Collections Actionscript 3

查看:184
本文介绍了不可变集合Actionscript 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在尝试在AS3中实现一些干净的编码实践。其中一个是不从包含对象中释放对数组的引用。关键是我控制添加和删除从一个类和所有其他用户的Array接收只读版本。

I've been trying lately to implement some clean coding practices in AS3. One of these has been to not give away references to Arrays from a containing object. The point being that I control addition and removal from one Class and all other users of the Array receive read only version.

当前只读版本是 ArrayIterator 类,它实现了典型的 Iterator 接口(hasNext,getNext)。它还扩展了 代理,所以它可以在中用于每个循环,就像数组一样。

At the moment that read only version is a ArrayIterator class I wrote, which implements a typical Iterator interface (hasNext, getNext). It also extends Proxy so it can be used in for each loops just as a Array can.

问题是这不应该是许多语言的基本特征吗?传递引用只读集合的​​视图的能力?

So my question is should this not be a fundamental feature of many languages? The ability to pass around references to read only views of collections?

现在,对于AS3中的集合,在 Vector 类的形式中有改进的类型安全性,当我将aa VectorIterator 为了不变性,我输了。有没有办法实现这两个欲望,不变性和输入AS3?

Also now that there is improved type safety for collections in AS3 , in the form of the Vector class, when I wrap a a Vector in a VectorIterator I lose typing for the sake of immutability. Is there a way to implement the two desires, immutability and typing in AS3?

推荐答案

看起来使用 迭代器 模式是当前在AS3中传递集合的最佳方式系统,同时保证不会被修改。

It seems that using an Iterator pattern is the best way currently in AS3 to pass a collection around a system, while guaranteeing that it will not be modified.

我使用的 IIterator 接口是建立在Java 迭代器,但我不实现 remove()方法,因为这被认为是设计错误的许多人在Java社区,由于它允许用户删除数组元素。以下是我的 IIterator 实施:

The IIterator interface I use is modeled on the Java Iterator, but I do not implement the remove() method, as this is considered a design mistake by many in the Java community, due to it allowing the user to remove array elements. Below is my IIterator implemention:

 public interface IIterator
 {
     function get hasNext():Boolean
     function next():*
 }

然后通过ArrayIterator,VectorIterator等类实现。

This is then implemented by classes such as ArrayIterator, VectorIterator etc.

为了方便起见,我还在我的具体Iterator类中扩展了 通过覆盖 nextNameIndex() nextValue()方法,为AS3中的 for-each 这意味着通常使用Arrays的代码在使用我的 IIterator 时不需要更改。

For convenience I also extend Proxy on my concrete Iterator classes, and provide support for the for-each loops in AS3 by overriding the nextNameIndex() and nextValue() methods. This means code that typically used Arrays does not need to change when using my IIterator.

var array:Array = ["one", "two", "three"]

for each (var eachNumber:String in array)
{
    trace(eachNumber)
}

var iterator:IIterator = new ArrayIterator(array)

for each (var eachNumber:String in iterator)
{
    trace(eachNumber)
}

只有问题是...用户无法查看 IIterator 界面,并且知道他们可以使用 循环以遍历集合。他们必须看看ArrayIterator的实现才能看到这个。

Only problem is... there is no way for the user to look at the IIterator interface and know that they can use a for-each loop to iterate over the collection. They would have to look at the implementation of ArrayIterator to see this.

这篇关于不可变集合Actionscript 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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