Search Flex Components Free

Custom Search

December 10, 2007

Proxy Class for Flash

Proxies are a handy concept in any programming language but I have found them especially useful in Flash when dealing with references.&

Proxy Class example:
http://www.powersdk.com/sample/proxy.zip

First a little primer on references in Flash:

There are essentially 3 types of references in Flash:

Primitives --> A = 3
Objects --> B = [1,2,3]
MovieClips --> C = _level0

In the above code, A, B, C are all references to data in memory. The Flash Player stores the data assigned to references in a pool of memory maintained by the Flash Player garbage collector. As long as a reference exists to memory, the garbage collector will not recycle the memory allocated. When you use 'delete' on a reference the reference is deleted not the target memory.

When you assign a second reference to an existing reference, the value of the reference is assigned to the new reference. Depending on what type of reference is assigned different things happen as follows:

Primitives: Primitives are assigned by value. When they are assigned,it is identical to creating a new reference and assigning value and type.

// create reference A
// assign value of 3
// assign type Number
A = 3

// create reference AA
// assign value of 3
// assign type Number
AA = A

Objects: Objects are assigned by reference


// create reference B to a new space in player memory
// create an array in the player memory assigned to B
B = [1,2,3]

// create reference BB and assign it to the memory allocated to B
BB = B

MovieClips: MovieClips are assigned by reference to a unique depth (slot)

// create reference B to the MovieClip assigned to _level0 (unique by depth!)
C = _level0

// create reference CC and assign it to the memory allocated to C
CC = C

============================

So now onto Proxy. A proxy is different as it is an object that actually points to another object. When references are assigned to a proxy, they are assigned to the proxy not the target object. If the object behind the proxy changes, the existing reference points to the new value. Normally, older references remain glued to the older value as follows:

//create a new array
A = [1,2,3]

//create a reference to A
B = A

//create a new array
A = [20,30,40]

trace(B[0]) //1
trace(A[0]) //20

When A was overwritten with a new value, the B reference maintained the link to memory originally allocated to A.

Let use a Proxy:

http://www.powersdk.com/sample/proxy.zip


//create a Proxy with a value of [1,2,3]
A = new Proxy( [1,2,3] )

//create a reference to the Proxy
B = A

//trace the value through the proxy references
trace(A[0]) //1
trace(B[0]) //1

// change the Proxy value
Proxy.setValue( A , {name:23} )

//trace the value through the proxy references
trace( A['name'] ) //23
trace( B['name'] ) //23

//obtain the true Proxy value
myProxyValue = Proxy.getValue( A )

The proxy allows you to create references that change with the value assigned prior. They are very unique and provide a powerful programming concept in Flash. In other languages ( Java, C#, Python ) Proxy types allow you to control the Proxy instance interface and restrict usage in unique ways. A Proxy is a very clean way to provide mix-in classes, remoting, and essentially hide implementation details in larger scale application development.

There are several serious problems with this implementation of a Proxy:

1. Performance - Proxy uses __resolve to provide proxy behavior. Every time the proxy is used the __resolve method is fired within the Proxy Class. This is performance intensive and widespread use of this Proxy class would be sure to create performance problems in an application.

2. Semi-Transparent - It is fairly easy to see that you are dealing with a Proxy instance. Ideally proxies instances are seamless and you cannot tell them from the real data object. In the above case, a simple trace on the Proxy itself will show the facade presented. Also since the Proxy uses __resolve, the proxy interface is distorted as it will always include this extra method.

3. Activation Object - Since __resolve needs to be unique for each Proxy instance, a function was assigned in the Class constructor thus binding the Activation Object into the Proxy Instance. There is not other way to get Proxy behavior without triggering this issue.

4. No Primitive Support - It is difficult to proxy primitive values as they are passed by value not by reference. Accessing the primitive value forces the use of the valueOf method and can cause problems.

I would like to see a native proxy class for Flash 8 that resolved the above issues and extended the capability for using Proxy types in this manner. A native extensible Proxy class would allow us to define new proxy functionality and create patterns within Flash that make software development much cleaner and seamless.

If you would like to see a native Proxy Class in Flash 8, please add a comment to this Blog entry. Hopefully with community support and discussion, we might see this jewel added into the new Flash 8 Player.

Related Flex Tutorials