In AS3, EventDispatcher
is an implementation of the observer design pattern. This class implement the addEventLister
, removeEventListener
, dispatchEvent' and
hasEventListener` methods. Internally, it also maintains a dictionary or similar data structure that contains the events which are currently being listened for, and a list of methods which have to be called when the event is dispatched. Something like this -
{"event1": [method7, method5, method3], "event2": [method3, method2], "event3": [method1]};
When addEventListener
is called on an object, it creates a new key for the event in question and adds the method reference to its associated value list.
When dispatchEvent
is called on the class, it fetches all the methods associated with the event and calls the methods attached with it. Each method is called with an instance of the Event
class or its subclasses.
Removing an event listener obviously does the opposite of what adding does.