Delegate
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Implements: | |
| Version: | 1.0 |
| Author: | Francis Bourre |
| Classpath: | com.bourre.commands.Delegate |
| File last modified: | Wednesday, 31 October 2007, 07:35:29 |
Delegate was originally created by Mike Chambersfor Macromedia mx.events package.
As he said, this is a reusable class that acts as a proxy for events,
registering itself for specific events, and then proxying
the calls to the event handler and scope you specify.
This provides the following benefits and advantages :
- You can dispatch change events from two different components to two separate methods.
- You can define the scope in which the event handler would be called (in this case, the containing class).
- Because it is a separate class, it was modular and you can easily reuse it across projects.
For more explanations and a complete tutorial,
see there.
This version is also inspired from Peter Hall's EventDelegate.
You can instantiate and keep a reference of a Delegate instance.
For more informations, see there.
So, what are the new additions ?
- You can pass parameters and add some more further.
See examples below.
- One of the greatest feature is Command pattern implementation.
See classes documentation below to figure out benefits :
- com.bourre.commands.ThreadManagerFPS
- com.bourre.commands.ThreadManagerMS
- Batch
Here is a basic example with v2 Button component and setArguments use :
import com.bourre.commands.*; import mx.controls.*; function test(event, a) : Void { event.target.label = String(++a); d.setArguments(a); } var d:Delegate = new Delegate(this, test); var pb:Button = createClassObject(Button, '__pb', 10, {label:'0'}); pb.addEventListener('click', d); d.setArguments(0);
Summary
Constructor
Delegate
Delegate instance.Class methods
create
Creates a method that delegates its arguments to a specified scope.
Instance methods
addArguments
Here is a basical example with Delegate.addArguments :
import com.bourre.commands.*; function test() : Void; * { trace(arguments); } function temporisation(); * { d.execute(); d.addArguments( Math.round(Math.random()*9) ); } var d:Delegate = new Delegate(this, test, 0); setInterval(temporisation, 100);
callFunction
No return type is provided for interfaces compatibility.
execute
Command polymorphism.
getFunction
var d:Delegate = new Delegate(this, test, "hello world");
function test( s:String ) : Void; * {
trace( s );
}
mc.onPress = d.getFunction();
getScope
setArguments
See examples above.