Introduction

You are currently browsing the weblog archives for May, 2005.

Archive for May, 2005

Tuesday, May 31st, 2005

As2lib Event API

This tutorial covers event basics, different approaches to event handling, their advantages and disadvantages and how these approaches were realized in the As2lib Event API (org.as2lib.env.event).
Note that this article has also been published on the official as2lib homepage: As2lib Event API. You may find it better structured there. If you are familiar with the old already known event approaches you may just scroll down until the “Distributor” part.

Event Basics

What is an event? When are events used? Are events related to the Observer Design Pattern of GoF (Gang of Four)?

An event is every change in an application an event handler receives. Events are for example triggered if a certain point in an application is reached or if a user interacts with the application. In theory everything can trigger events. An event-driven API normally consists of two main modules.

  • A dispatcher is responsible for dispatching the event to multiple event handlers. Dispatchers are also called broadcasters, distributors, publishers and multicasters.
  • Event Handlers are responsible for handling events. They receive the event and operate in accordance with it. They may also consume the event and thus stop the event to be dispatched to further event handlers. Event handlers are also called listeners, subscribers and observers.

In most event APIs, event handlers can register themselves at a dispatcher. If an event occurs the dispatcher then dispatches the event to all registered event handlers. Thus the dispatcher has two main responsibilites.

  • Managing a collection of event handlers.
  • Dispatching events to all registered event handlers if they occur and maybe managing an event queue that holds unprocessed events.

Some dispatchers also support the consumption of events. The dispatcher then dispatches an event to event handlers step-by-step, until an event handler consumes the event. This is sometimes called to veto to an event.

Events are used when there is more than one object or there are an unknown number of objects that may want to be notified if some specific operation took place. For example if a user pressed a button. It is bad pracitice to use events for every interaction between objects because this adds unnecessary complexity and slows the execution of the program down. So, think carefully if using events really benefits your application in your specific case.

The Observer Design Pattern is closely related to events. The “Subject” is analog to the “Dispatcher” and the “Observer” to the “Event Handler”. Reading the Observer’s intent you can clearly see the connection: “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.”.

Listener Source

What is a listener source? Which listener source implementations are provided by the As2lib Event API and what are their differences?

In the As2lib Event API we call event handlers listeners and thus a collection of event handlers listener source. The “EventListenerSource” interface declares common methods needed to manage a collection of listeners. A listener source is so to speak a data holder that holds listeners. The As2lib Event API offers two types of listener sources.

  • The “SimpleEventListenerSource” manages listeners in the simplest way possible.
  • The “TypeSafeEventListenerSource” manages listeners and ensures that it contains only listeners of a specific type.

Depending on your event handling implementation you may not need a type-safe listener source because you may have ensured the correct type of the listeners somehow else. In such a case use the simple listener source or the type-safe listener source with a flag that the type shall not be checked. Otherwise use the type-safe listener source because it prevents unexpected behavior caused by wrong listeners.

Broadcaster

What is the As2lib Broadcaster Event API (org.as2lib.env.event.broadcaster)? What are its advantages and disadvantages compared to other event APIs? In which scenarios is it worthwhile to use this API?

This As2lib Broadcaster Event API is built around the “EventBroadcaster” interface. This interface combines listener source and dispatch functionalities. The “dispatch” method expects exactly one parameter that must be of type “EventInfo”. This event info is also passed-to the listeners. It holds at least the name of the event that is also the name of the method to invoke on the listeners, the event method. It may also contain any additional information. The normal process when working with this API is as follows:

  • Creating a listener interface that declares the event method. This event method takes one parameter that must be of type “EventInfo” or a sub-interface or implementation-class.
    1. interface MyListener {
    2.     public function onMyEvent(myEventInfo:MyEventInfo):Void;
    3. }
    4.  
  • Creating the specific event info class that has been used as type for the event method’s parameter. This class must implement the “EventInfo” interface. The “getName” method should return a hard-coded string, the name of the event method.
    1. class MyEventInfo implements EventInfo {
    2.     private var information:String;
    3.     public function MyEventInfo(information:String) {
    4.         this.information = information;
    5.     }
    6.     public function getInformation(Void):String {
    7.         return this.information;
    8.     }
    9.     public function getName(Void):String {
    10.         return "onMyEvent";
    11.     }
    12. }
    13.  

In your application you instantiate an event broadcaster for every event, listeners can register to and implement the appropriate add and remove methods that should be properly typed to the listener interfaces. Now when an event occurs you instantiate the appropriate event info with all needed information and dispatch to the added listeners with the appropriate broadcaster passing-in the instantiated event info.

  1. class MyClass {
  2.     private var myBroadcaster:EventBroadcaster;
  3.     public function MyClass(Void) {
  4.         this.myBroadcaster = new SpeedEventBroadcaster();
  5.     }
  6.  
  7.     private function dispatchMyEvent(information:String):Void {
  8.         this.myBroadcaster.dispatch(new MyEventInfo(information));
  9.     }
  10.  
  11.     public function addMyListener(myListener:MyListener):Void {
  12.         this.myBroadcaster.addListener(myListener);
  13.     }
  14.  
  15.     public function removeMyListener(myListener:MyListener):Void {
  16.         this.myBroadcaster.removeListener(myListener);
  17.     }
  18. }

The advantages of this approach are:

  • If the event method’s name of a listener changes there is only one place to change the event method’s name, the event info class.
  • Because of your typed methods to add and remove listeners you ensure that only listeners that really implement the expected interface are added, so there is no need for further type-checking by the listener source, which leads to better performance.
  • Because of your event info class that contains all event specific information you are not bloating the event method’s parameter list. Note though that in the above case this would not have been necessary because we have only one information.
  • This approach is rather fast because it does not have to make any detours.
  • This approach does not prescribe the existence of a listener interface or class. Note that I myself think of not declaring an explicit listener type as bad practice.
  • If one of the listeners needs more information one can simply extend the event info without having to alter other listeners.

But there are also many disadvantages:

  • The compiler does not check whether the event method’s name on the listener and the one in the event info class are in sync.
  • The listener must always get its information from the passed-in event info. If there is no information needed or if the information needed is only one value, as in the above example, an event info is overkill and unintuitive.
  • The listener is bound or has a dependency to the As2lib Broadcaster Event API because it declares one parameter of type “EventInfo”.
  • There must be one, in some cases unnecessary class, the “EventInfo” implementation.

Let’s move on and take a look at the As2lib Multicaster Event API that eliminates some of the disadvantages, but adds some new ones.

Multicaster

What is the As2lib Multicaster Event API (org.as2lib.env.event.multicaster)? What are its advantages and disadvantages compared to other event APIs? In which scenarios is it worthwhile to use this API?

The core interface of the As2lib Multicaster Event API is the “EventMulticaster”. This interface combines, like the “EventBroadcaster” interface of the previous tutorial page, listener source and dispatch functionalities. The difference is that this time the “dispatch” method expects any number of parameters of any type that are passed-to the event methods of all registered listeners; the listener interface is thus not bound to an event info and a specific event API. All the rest stays basically the same.

  1. interface MyListener {
  2.     public function onMyEvent(information:String):Void;
  3. }

  1. class MyClass {
  2.     private var myMulticaster:EventMulticaster;
  3.     public function MyClass(Void) {
  4.         this.myMulticaster = new SimpleEventMulticaster("onMyEvent");
  5.     }
  6.  
  7.     private function dispatchMyEvent(information:String):Void {
  8.         this.myMulticaster.dispatch(information);
  9.     }
  10.  
  11.     …
  12. }

The advantages are:

  • We do not bind our listeners anymore to a specific event API by allowing custom parameters.
  • We can still package information/parameters into one data transfer object if we want to, but it is not prescribed. And this object also does not have to extend or implement a specific class or interface.
  • Dispatching events and declaring listeners feels much more natural and is more intutitive.

But again this is connected with some disadvantages:

  • The name of the event method is in most implementations passed-in on instantiation of every event multicaster. This is more error-prone because it is easy to make a typo. If the listener’s event method’s name changes we also have to change the event method name in multiple locations.
  • Compile-time checking of whether the event method’s name on the listener and the ones we pass-to our event multicasters is still not given.

Again, there issstill much that can be improved; most notably the lack of compile-time checking. This issue is addressed by the As2lib Distributor Event API, so go on.

Distributor

What is the As2lib Distributor Event API (org.as2lib.env.event.distributor)? What are its advantages and disadvantages compared to other event APIs? In which scenarios is it worthwhile to use this API?

Unlike the previously discussed event APIs, this API’s base interface is not a dispatcher but a dispatcher control: “EventDistributorControl”. This interface acts as a listener source and a provider and controller of a typed distributor. You again create such a control per event providing it the listener interface or class.

  1. var myControl:EventDistributorControl = new SimpleEventDistributorControl(MyListener);

You can then use this control to add and remove listeners.

  1. myControl.addListener(new SimpleListener());
  2. myControl.addListener(new ComplexListener());

If the event occurs you first get a typed distributor from the control and invoke the event method on it passing-in the appropriate parameters. The neat thing about this is that you get compile-time checking whether the method you are invoking exists and whether the parameters are of correct type.

  1. var distributor:MyListener = myControl.getDistributor();
  2. distributor.onMyEvent("myArg1", 2, new Object());

The advantages are obvious:

  • You have proper compile-time checking when dispatching the event; type-checking of parameters and existence-checking of method.
  • You can have proper type-checking of listeners because you specified the expected listener type.
  • You can use custom parameters and are not bound to an event info. But if you want to package the information in one object you are free to do so; this object must not extend or implement any specific class or interface, its just a design choice.
  • The listener is not bound to any specific event API.

One disadvantage may be that if the name of the event method on the listener changes you have to make a change in every place where you dispatch events. But the compiler tells you that and where these changes have to be made. A good IDE also changes all calls to the method automatically if you change the method’s name. So, this is not much of an issue.

Tuesday, May 31st, 2005

As2lib 0.9 Release

I am proud to finally announce a new release of the As2lib after 9 months of hard work to make it faster, lighter, more stable and more intuitive.

Download As2lib Version 0.9.

The most important changes are:

  • Mtasc and Flex compatibility
  • Unit tests for the whole framework to make it much more stable
  • Modularization of the framework to reduce file size when only using a sub-set of its different parts
  • Good api documentation and easy online access

The most noticeable features are:

  • An event api split into three parts to serve different preferences and needs - org.as2lib.env.event
  • An exception api that gives comprehensive information about the problem through a detail message, an exception cause and a stack trace - org.as2lib.env.except
  • An improved logging api that is easy to use and fast; It allows for easy inclusion of other logging apis and consoles - org.as2lib.env.log
  • Better overloading capabilities of the overloading api - org.as2lib.env.overload
  • Fast and easy to use reflection api - org.as2lib.env.reflect
  • A comprehensive set of data holders/structures and iterators needed in daily work - org.as2lib.data.holder
  • Different mainly numerical type formats - org.as2lib.data.type
  • A local connection api that allows for return values and even sends exceptions over the wire - org.as2lib.io.conn.local
  • A unit testing api that allows for easy asynchronous testing - org.as2lib.test.unit
  • A mock object api that can be easily integrated with the unit testing api - org.as2lib.test.mock
  • A speed test api (profiler) - org.as2lib.test.speed
  • Support for processes and easy configuration - org.as2lib.app
  • Different utilities for strings, objects, classes, arrays and a date formatter that do simple but often needed tasks - org.as2lib.util

I hope you will enjoy the new features and give much feedback.