Thursday, May 2, 2013

The observer pattern in different programming languages

It's time for a more technical post. I have to admit, it seems like I lost focus on what I wanted this blog to be and from now on I will post more technical stuff here – as well as continuing with reviews, but focusing on more interesting review topics ;)

Some time ago I realized that when working with events and using the observer pattern (see GoF - Observer pattern), there is a huge difference between programming languages. I never really thought about it, but when you work on multiple platforms using different programming languages, you realize, how different this simple pattern can be.
The programming languages which I want to compare are ActionScript, Java and C#. Let's start with the descriptions of the implementations and after that I will talk about advantages and disadvantages.


ActionScript
Basically it's very easy to use and implement events using ActionScript. In order to dispatch events you have to inherit from the class EventDispatcher (or one of its subclasses) to get access to the method dispatchEvent(e:Event). This method takes an Event object and then dispatches events to observers. There are already some subclasses of Event you can use if the event includes data that the observers should receive, for example DataEvent which includes a string data field. If you want developers to be aware of events an object dispatches and provide them with better code completion (at least in Adobe Flash Builder), you can add the following metadata to the class:
[Event(name="dispatchedEventName", type="flash.events.Event")]
The name of the event is the same as the Event object's type name and the type is the actual ActionScript datatype of the dispatched event.

Observing events is quite easy too: just create a new object and add or remove an event handler method for a specific event using the methods provided by the subject's base class EventDispatcher:
addEventListener(type:String, listener:Function) and removeEventListener(type:String, listener:Function)
Again, the type of the event is the Event object's type name.

Java
In Java it is possible to use a similar approach as with ActionScript: there is a class java.util.Observable that a class can extend which provides methods for registering, unregistering and notifying observers (addObserverdeleteObserver and notifyObservers). In this case observers are classes that implements the interface java.util.Observer to receive notifications about events when the method update is invoked. You can of course also implement your own custom observer base class and observable interface.

But wait! When you work with JavaBeans it's not the same. A JavaBean requires the developer to follow naming conventions: for (un)registering an observer you have to implement a method add/remove<Event>Listener(<Event>Listener a). Now you can just use an Observable object to delegate the functionality. Instead of Observer objects you have to create a new interface that extends Observer but name it according to the naming conventions.

C#
Last but not least the observer pattern in C# is quite simple too. Again there are two possible solutions: you can use the IObserver and IObservable interfaces and implement them (very similar to Java) or you could use "real" C# events.

At first you have to declare a delegate that describes the interface of the observer's callback method (1). Then you can use this delegate as the datatype of an event property/field (2). To fire the event you just have to check if the field isn't null and call it like a method (3).
Registering and unregistering is very easy. Just add or remove observers (4).
public EventDemo {
(1)  public delegate void AHappenedDelegate(A a);
(2)  public event AHappenedDelegate AHappened;
  protected void DoAHappened() {
    if (AHappened != null)
(3)      AHappened(new A());
  }
}
public void SomeAHappenedDelegate(A a) {...}
(4) AHappened -= SomeAHappenedDelegate;
AHappened += SomeAHappenedDelegate;

Comparison
Basically, most approaches are quite similar. The approach I like the most is the C# events, because you have a dedicated event type for registration and dispatching. It's really easy to use and you have the necessary syntactic sugar ;)
Everything else is nice, but could be better. Especially when working with Java, it's kind of annoying to only have naming conventions and no real guarantees on implementing it correctly – except maybe via IDEs.

I hope you liked this quick comparison and next time you work with events, think about it and maybe you know some completely different approach you would like to share :)

Labels: , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home