AS3 で Proxy クラスを継承しつつ IEventDispatcher を実装
してみた。
Proxy つかいたいけど、EventDispatch もやりたい! っていう贅沢な感じ。
AS が多重継承できればすぐなんだけど、そんなことできないから Proxy クラスを継承して、IEventDispatcher を implements する。
IEventDispatcher はそんなに複雑なことしてなくて、中でEventDispatcher使ってゴニョゴニョする。なんだか再帰的なイメージ。
特別実装できない、というわけじゃなければ素直にEventDispatcherを継承したほうがよさげ。
具体的には、クラス内にEventDispatcherオブジェクトを保持して、こいつに肩代わりしてもらうだけっぽい。
package { import flash.utils.Proxy; import flash.utils.flash_proxy; import flash.events.IEventDispatcher; import flash.events.EventDispatcher; dynamic class Unko extends Proxy implements IEventDispatcher { private var dispatcher:EventDispatcher; public function Unko() { super(); dispatcher = new EventDispatcher(); } /* override Proxy method */ override flash_proxy function callProperty(methodName:*, ... args):* { // do something } /* override Proxy method */ override flash_proxy function getProperty(name:*):* { // do something } /* override Proxy method */ override flash_proxy function setProperty(name:*, value:*):void { // do something } /* implements IEventDispatcher */ public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void { dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); } /* implements IEventDispatcher */ public function dispatchEvent(event:Event):Boolean { return dispatcher.dispatchEvent( event ); } /* implements IEventDispatcher */ public function hasEventListener(type:String):Boolean { return dispatcher.hasEventListner(type); } /* implements IEventDispatcher */ public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { dispatcher.removeEventListener(type, listener, useCapture); } /* implements IEventDispatcher */ public function willTrigger(type:String):Boolean { return dispatcher.willTrigger(type); } } }
使い道としては、SharedObject みたいなどんなプロパティでも入れられちゃうようなクラスをプロキシして、set されたら change イベントをディスパッチするみたいなところとか。
・・・他は自分の頭では思いつきませんw
だけど、IEventDispatcher の使い道は他にも見いだせそう。