Take the 2-minute tour ×

I'll try to be objective :) I would like to know how and why someone will need to create you own event. I actualy know how to do it but I don't get the really idea of do such a thing. Could you give me an example with a explanation of why create your own event!?

I look up for that answer on the web, but I never found it...

And sorry about my english :)

share|improve this question

4 Answers 4

Lets try to compare the observer pattern with a "manual" object notification system... I'm sure everybody has been guilty of this when they started programming.

public class GameX{
   public function GameX(){
      var b = new Button(this); //we send a reference to button
   public function buttonAction(){
       //do something based on this

public class Button(){
   private var listener:
   public function Button(listener:GameX){
      this.listener = listener;
   public someButtonThing(){

Lets iterate through problems one would encounter with this code...

1) I want to use the Button class in another game, GameY? Is that a problem?

Sure is, compile time dependency on GameX inside the button class... yeah, you could change this or cheat in some other way, but that is not easy or standardized... and standardization is probably one of the key things of the observer pattern... why it's so good. This also breaks encapsulation... more on that in a second.

2) I want to listen from GameYComponent1 and GameYComponent2 at the same time! Can I do it?

Again, you could "cheat" and change this code, but again, you have to change the Button class and, again, it would not be as standardized as the observer pattern (the events thing). And many times you simply cant... the best you can do is subclass from it. To much work. Also not standardized.

3) You want some kind of a very complex event dispatch mechanism, like the display object list in as3 has (sorry, I can't find a good link for this on the net right now, if somebody can edit this later please to add the link in).

I could iterate until tomorrow and there are people that can explain it better than me, but basically, not using the observer pattern will probably break encapsulation and force you to modify the Button class over and over again... it would also not be as standardized as events would be... no need to reinvent the wheel, the observer pattern was created to address these kinds of problems.

In any case, this is how I would explain to a guy new to programming in under 15 minutes why to use events, or custom events. It avoids a lot of headache for everybody involved.

For a more comprehensive study try these links please (I just glossed over these things, but there should be some truth in here):

If somebody has more, better links to add, feel free.

Also, it's good because it helps with encapsulation a little bit, the less Button class knows about the world and vice versa the better (without going into details, and again, somebody can probably explain this better):


Also, something I keep mentioning but not in detail, everybody with a little bit of experience understands events and can follow them. Events can make code a pain to understand to new programmers, and are harder to follow versus concrete reference links, but the costs outweigh the benefits. That is what I mean when I say that standardization of the observer pattern is probably one of the keys of it's importance.

Tell me whether it makes sense to you.

share|improve this answer
Also, consider this a part of the answer though I do not want to bloat it (basically, don't repeat yourself): [en.wikipedia.org/wiki/Abstraction_principle_(programming)] –  Neoraptor Jul 21 '11 at 21:10

I create my own events for everything. Say for example, I have a Sprite which has a child Sprite. The child Sprite is changing color all the time based on an array of color values. I want to inform certain other sprites, that this sprite is now red but I don't know when it will happen because it is iterating through the color array randomly. The child sprite can fire an event when that condition is met, and anyone who is listening can react upon it.

Otherwise, everyone who needs to know when that Sprite is red will have to ask the child, are you red, are you red, are you red?

share|improve this answer

A post I made while back on custom events.

Custom events simply give you much more control over the event structure in your app. Maybe most importantly, is the semantic relevance to this or that operation: XML loaded successfully? fire an MyEvent.XML_LOAD_COMPLETE.

As I detail in the post, you can also extend out custom events with new functionality, specifically, in my case, to handle extra data along side of the fired event.

Which is ridiculously handy.

Hope that helps

share|improve this answer

I will create my own event when I need to store more information on the event object. That way when I handle the event in the listener I have access to the required data without having to store it on the event.target object.

As a side note I have found the signals library to better achieve this.

share|improve this answer
+1 for signals : ) –  citizen conn Jul 21 '11 at 20:15

Your Answer


