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