Ted Patrick > { Events & Community } > Adobe Systems


Magic with flash.util.Proxy

There are so many new classes in AS3 but I have been exploring flash.util.Proxy lately. This is one crazy class and very useful if you know what your doing. With Proxy you can control how instances behave at a very low level.

You never use Proxy directly, you always subclass it and override methods to control behavior. Essentially any type of access to an instance has a method to override in Proxy. Think of it as the base interface on any object within the Flash Player at runtime. Here are a few examples, assume that myInstance is an instance of a Proxy Subclass:

// GET operation
myInstance.dog;
// identical to calling: (must be called from the flash_proxy namespace!!!)
// flash_proxy myInstance.getProperty( 'dog' )

// SET operation
myInstance.dog = 1;
// identical to calling: (must be called from the flash_proxy namespace!!!)
// flash_proxy myInstance.setProperty( 'dog' , 1 )

// CALL operation
myInstance.dog( 1 , 2 , 3 );
// identical to calling: (must be called from the flash_proxy namespace!!!)
// flash_proxy myInstance.callProperty( 'dog' , 1 , 2 , 3 )

There are several other methods to override in proxy which allow you to control the following:

callProperty - a method operation
deleteProperty - a delete operation
getDescendants - E4X Related operation
getProperty - a get operation
hasProperty - a has operation
isAttribute - testing the presence of an attribute in XML/E4X
nextName - enumeration support
nextNameIndex - enumeration support
nextValue - enumeration support
setProperty - a set operation

Using Proxy is best reserved for situations when you want very fine grained control of behavior. The are very useful for dynamic method calls, like WebServices, Remoting, or in general creating a Facade for any type of object.

SINGLETON implemented via flash.util.Proxy

WARNING... THIS IS A TEST, IT IS NOT INTENDED FOR ACTUAL USE!!!

In exploring Proxy, I created a Singleton implementation using flash.util.Proxy as a test, yes a test. The goal was to see if I could use the 'new' operator during Singleton instantiation such that any call to new SingletonClass() would emit a Singleton with the exception of the first call when a new instance of the Singleton value could be passed in. It works but it has some subtle holes in the implementation. Hopefully showing this publicly can highlight the holes and might push this into a usable implementation.

SINGLETON PROXY in AS3

import com.ted.pattern.*
import flash.util.*

//create an instance passing a new Array
a:SingletonProxy = new SingletonProxy( new Array() )

//call some operations
a.push( 'Hello' );
a.push( 2 );
flash.util.trace( a[1] ); //2

//create a 2nd Singleton instance
b:SingletonProxy = new SingletonProxy();

trace( b.length ); //2
trace( b[0] ); //1
trace( b[1] ); //2

//comparison operations fail unless valueOf is called
trace( a == b ); //false
trace( a === b ); //false
trace( a.valueOf() == b.valueOf() ); //true
trace( a.valueOf() === b.valueOf() ); //true

In summary, flash.util.Proxy is a very very handy class to understand. It is very useful in controlling object behavior and is widely used within the Flex Framework. Actually many the mx.collections.* Classes are based on it. :)

The more I dig into AS3 and Flex2, the better it gets.

Cheers,

Ted :)

6 Responses to “ Magic with flash.util.Proxy ”

  1. # Anonymous David R

    It seems this class is like Object.__resolve on steroids. I like the support for capturing enumeration as well.  

  2. # Blogger Ted Patrick

    David,

    You nailed it. That is exactly the purpose of flash.util.Proxy!

    Ted :)  

  3. # Blogger Ted Patrick

    NOTE: I just updated the AS3 Singleton implementation with comments.

    Ted :)  

  4. # Anonymous Kevin

    Ted,
    Very cool indeed, but seems a little confusing still. Basically, I want to replicate Xstream in AS3 without having to modify my AS value objects (classes). Is that possible without extending each of my model classes from Proxy and implementing the methods? Ideally, it'd work like Java where the domain objects remain intact and reflection can easily create the class and call the get/set method on the property.  

  5. # Blogger Chunks

    Hi Ted,

    I am using Flash 8 and AS 2.0. I am currently developing widgets. But the problem that I am facing is with the web proxy in my organization. It blocks any RSS feed from a sports or video (like youTube) website. Is there a way in flash that I can bypass the web proxy and load the feed.
    Thanks.  

  6. # Anonymous Dave

    Wrong kind of Proxy Chunks :)

    Dave  

Post a Comment



© 2008 Ted On Flash