org.as2lib.core.BasicInterface +--org.as2lib.env.event.EventListenerSource +--org.as2lib.env.event.distributor.CompositeEventDistributorControl
CompositeEventDistributorControl
allows flexible usage of events for complex class models.
The EventDistributorControl class allows only for handling of one type of
listeners while this CompositeEventDistributor
class allows multiple types
of listeners to provide more granularity if many different kinds of listeners are
used. It holds a collection of accepted types of listeners and checks if the listener
added via the addListener method matches any of the accepted types and adds
it to the correct event distributor control(s).
Class that uses the composites functionalities:
import org.as2lib.env.event.distributor.SimpleConsumableCompositeEventDistributorControl;
class MyClass extends SimpleConsumableCompositeEventDistributorControl {
public function MyClass(Void) {
acceptListenerType(MyListener);
}
public function customMethod(Void):Void {
var e:MyListener = getDistributor(MyListener);
e.onEvent("1", "2");
}
}
Listener interface:
interface MyListener {
public function onEvent(contentA:String, contentB:String):Void;
}
Listener interface implementation:
class SimpleMyListener implements MyListener {
private var prefix:String;
public function SimpleMyListener(prefix:String) {
this.prefix = prefix;
}
public function onEvent(contentA:String, contentB:String):Void {
trace(prefix + contentA + ", " + prefix + contentB);
}
}
Usage:
var myClass:MyClass = new MyClass();
myClass.addListener(new SimpleMyListener("a"));
myClass.addListener(new SimpleMyListener("b"));
// traces "a1, a2" and "b1, b2";
myClass.customMethod();
// throws an exception because listeners of type "Array" are not accepted
myClass.addListener(new Array());
addAllListeners(), addListener(), getAllListeners(), hasListener(), removeAllListeners(), removeListener()
public function getDistributor(type:Function)
Returns the distributor for the given type
that can be used to distribute
events to all added listeners of the given type
.
The returned distributor can be casted to the given type
(type-safe
distribution of events).
the distributor to distribute events
public function acceptListenerType(listenerType:Function):Void
Specifies that listeners of the given type
are accepted, this includes
implementations of the given type
as well as its sub-classes.
addListener
does not allow listeners that do not match (instanceof)
at least one accepted listener type.
listenerType | the type of listeners that can be added |
public function registerEventDistributorControl(eventDistributorControl:EventDistributorControl):Void
Registers the given eventDistributorControl
with its listener and
distributor type returned by its EventDistributorControl.getType method.
If there is already a distributor control registered for the given type, it will be overwritten.
You use this method if you have a specific event that should be executed with a special kind of distributor, for example with a consumable one.
eventDistributorControl | the event distributor control to use for event distribution for the given type |
public function registerDefaultEventDistributorControl(type:Function):Void
Registers a default event distributor control with the given listener and distributor type.
If there is already a distributor control registered for the given type, it will be overwritten.
type | the type to register a default distributor control with |