Introduction

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

Archive for May, 2005

Tuesday, May 31st, 2005

As2lib Event API

cheap cialis pill certified cialis cheap viagra in canada cialis buy drug buy generic cialis viagra buy 25mg viagra cheap viagra without prescription buy cheapest viagra on line purchase viagra cialis 10mg buying generic viagra cialis pills viagra from india cheapest sildenafil citrate cheap cialis no rx viagra india cialis bangkok viagra for order buy sildenafil internet buy generic viagra online buying cialis online where to order cialis tablet cialis find cialis no prescription required viagra cheap drug order cialis cheap online online pharmacy cialis cialis no rx order generic cialis price of cialis viagra soft drug viagra cheap viagra from uk order cialis no prescription order cheap viagra viagra drug order cheap cialis cheap cialis pharmacy best price for viagra cheap viagra from usa cost cialis cialis overnight shipping cheapest generic cialis online generic viagra online online viagra viagra sales cheap cialis in canada compare cialis prices online cialis online drug viagra online purchase discount cialis without prescription no rx viagra cialis overnight viagra uk cialis order cheap cialis from usa buying cialis cialis overnight delivery cialis in bangkok buy and purchase sildenafil online impotence treatment cheap price viagra viagra sale cheap cialis tablet drug cialis generic cialis online cheap viagra pharmacy find discount cialis online viagra malaysia cialis without a prescription buy cialis online cheap viagra rx buy no rx viagra cialis 20mg viagra in malaysia discount viagra online buy sildenafil cheap buy viagra low price buy cialis cialis cheap price cialis cheap generic viagra cialis canada low cost viagra buy cheap viagra cialis vs viagra order cialis from us cialis tablets find no rx cialis buy generic cialis online buy viagra overnight delivery cheapest cialis price buy cheapest cialis on line order cialis in canada viagra tablet viagra no online prescription find cheap cialis online viagra price order viagra no prescription cheap generic cialis buy viagra online cheap cialis uk cialis without rx generic cialis cheap viagra vs cialis order cialis on internet viagra tablets viagra purchase impotence drugs buy cialis generic cialis tablet cialis cheapest price order viagra from canada viagra generic cheap viagra from canada order cialis compare viagra prices online find cheap cialis impotence cure pfizer viagra find discount cialis cheapest cialis buy cialis from india impotence buy cheapest viagra online cialis side effects viagra order discount cialis online cialis in malaysia cialis in uk viagra in uk cialis online without prescription cialis online pharmacy order viagra buy viagra online viagra side effects cialis sale discount cialis no rx cheapest viagra find cialis order cialis no rx buy cialis low price buy viagra cheap drug cialis online purchase order discount viagra online 50 mg viagra 100 mg viagra 10mg cialis cost of cialis cheapest cialis prices buy discount viagra online cialis sales 50mg viagra cialis price buy viagra on internet cialis pill cheapest cialis online purchase viagra overnight delivery cheap cialis from canada cheapest viagra price cialis 20 mg buy sildenafil low cost order viagra without prescription buy viagra lowest price no prescription cialis order viagra on internet discount cialis overnight delivery cialis cheap drug viagra approved viagra no rx required compare viagra prices no rx cialis cheap cialis on internet buy viagra from india buy discount cialis online viagra pharmacy online order viagra from us cialis free delivery cialis for order buy cialis from canada viagra without rx viagra online review 10 mg cialis cheap viagra no rx cheapest viagra prices viagra prices cialis pharmacy order no rx cialis buy cialis in us buy cialis no prescription required order cialis from canada lowest price cialis cheap cialis internet online pharmacy viagra cheapest generic cialis generic drugs cialis india find cialis without prescription best price cialis buy viagra without prescription cheap cialis in uk where to buy viagra 20 mg cialis cheap cialis from uk buy sildenafil canada cialis no rx required cialis in us buy cialis overnight delivery cialis cheap price order cheap viagra online 20mg cialis buy cheap viagra online viagra internet viagra without prescription free cialis buy cialis us cialis buy buy viagra in canada order viagra cheap online find viagra without prescription viagra pills cheap cialis no prescription viagra online without prescription order generic viagra cialis discount viagra cheapest price purchase viagra no rx viagra no rx viagra cheap discount viagra overnight delivery sale cialis cialis pharmacy online purchase cialis without prescription pharmacy online cialis medication discount viagra buy cheap cialis impotence medication viagra medication find cialis on internet impotence pills cialis prices discount viagra without prescription cialis online cheap cialis online review find cheap viagra online buy viagra us purchase cialis online certified viagra where to order viagra buy cheapest viagra buy cialis internet order cialis online buy sildenafil online buy cialis cheap cheap viagra purchase cialis find discount viagra buy cialis on internet cialis buy online buy sildenafil online without a prescription viagra buy online order cheap cialis online viagra information no prescription viagra cost of viagra buy cialis in canada buy cialis online buy viagra cheapest generic viagra cialis us cialis australia fda approved cialis lowest price for viagra viagra bangkok cialis prescription cialis cost buy no rx cialis buy viagra internet viagra discount order viagra overnight delivery generic cialis viagra australia 25 mg viagra order viagra online viagra overnight cialis rx order cialis in us order viagra no rx order discount cialis online viagra vendors order viagra in us buy sildenafil in uk viagra us buy generic viagra viagra canada viagra no prescription viagra cheap price cheap viagra tablet viagra free delivery overnight viagra purchase viagra online find cheap viagra cialis malaysia best price viagra cialis free sample find viagra on internet cialis generic buy sildenafil in canada order cialis no prescription required cheapest viagra online purchase cialis no rx viagra in us order discount cialis cheap viagra internet free viagra cialis approved best price for cialis cialis from india find no rx viagra generic viagra viagra from canada viagra online pharmacy buy viagra from canada cheapest generic viagra online buy cheapest cialis discount cialis viagra overnight delivery cialis without prescription 100mg viagra cialis in australia price of viagra order cialis overnight delivery cheap viagra in uk buying generic cialis viagra pill buy cialis on line low cost cialis find discount viagra online buying viagra cheap cialis overnight delivery pharmacy cialis cheap viagra pill viagra prescription find viagra online buy cialis lowest price discount viagra no rx online cialis viagra free sample cheap viagra in usa find viagra cheap viagra online buy viagra no rx generic viagra cheap buy cialis without prescription buy viagra in us cheap viagra overnight delivery cheap cialis in usa cheap cialis online viagra order no rx viagra viagra soft tab find cialis online lowest price viagra cialis drug cialis vendors viagra online stores erectile dysfunction order viagra in canada buy viagra on line viagra overnight shipping viagra online cheap lowest price for cialis approved viagra pharmacy cialis 10 mg cialis no online prescription cialis purchase cialis from canada order cialis without prescription viagra for sale viagra in australia approved cialis pharmacy buy viagra generic buy sildenafil in spain find viagra no prescription required cialis no prescription buy viagra from us order viagra no prescription required cost viagra purchase viagra without prescription buy cialis no rx cialis cheap cialis internet tablet viagra cheap viagra on internet viagra cost pharmacy viagra cialis soft tab cialis information buy cheap cialis internet purchase cialis overnight delivery cheap cialis without prescription buy viagra no prescription required compare cialis prices buy cheap cialis online overnight cialis where to buy cialis cheap cialis buy cheap viagra internet buy discount cialis viagra buy drug cheap viagra no prescription buy sildenafil citrate buying viagra online buy discount viagra fda approved viagra cialis online stores cheap cialis tablets buy cheapest cialis online cheap viagra tablets order discount viagra sale viagra viagra online cialis for sale cialis soft viagra pharmacy buy cialis from us viagra without a prescription viagra in bangkok

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.