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

查看:126
本文介绍了不可变的集合的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 的类我写的,它实现了一个典型的迭代的接口(规则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收藏,在形式的向量的课,当我换了的 VectorIterator 的我失去了打字AA矢量为了不变性。有没有一种方法来实现这两个愿望,不变性和打字的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的迭代器,但我不落实的删除()的方法,因为这被认为是设计错误,许多在Java社区中,因为它允许用户删除数组元素。下面是我的 IIterator 的FPGA实现:

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.

为了方便我还延伸的代理的在我的具体迭代器类,并提供支持的的for-each 的通过覆盖的 nextNameIndex()循环在AS3 nextValue()的方法。这意味着code,通常使用数组不会用我的 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 的接口,并知道他们可以使用的的for-each 的循环来遍历集合。他们将不得不看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天全站免登陆