Ted Patrick > { Events & Community } > Adobe Systems


Duct Tape Observer

On Flashcoders, someone asked the difference between EventDispatcher and the Observer pattern. Patterns are guides, getting em to work practically is often a different story. Here is my take. Happy New Year!&

Duct Tape Observer

It isn't class based, it isn't pretty, but it works, just like Duct Tape. Just paste and compile.

SOURCE FLA

=============================================
//make sure to import EventDispatcher
import mx.events.EventDispatcher

//what we will watch for changes
theObserved = true

//create an ‘observer’
//this will watch ‘theObserved’ and tell anyone interested it has changed.
observer = {}

//add eventDispatcher methods to ‘observer’
mx.events.EventDispatcher.initialize( observer )

//add a watch to dispatch an event from observer
watch( "theObserved" , function(a,b,c){
observer.dispatchEvent( { type:"change" , name:a , newValue:c} )
})


//create a listener
//the 'observer' will tell this listener when something happens
listener = {}

//create an event to be called by the ‘observer’
listener.change = function(eo){
trace('The observer said "' + eo.name + '" changed to ' + eo.newValue)
}


//add the listener to the observer
observer.addEventListener('change', listener)

//change the value of 'theObserved' to test

theObserved = false
theObserved = true
theObserved = "OOOOH"
theObserved = "AAAAH"


=============================================

Now get out there and observe something!

Cheers,

Ted :)

10 Responses to “ Duct Tape Observer ”

  1. # Anonymous Anonymous

    hey, thanks a lot for posting this. i've wanted to use eventDispatcher for a while now, but didn't have a good model to start with. this is just perfect and i already know that i'll be using it in most all of my upcoming code.

    do you know if eventDispatcher gives any performance hits? is it memory intensive?  

  2. # Blogger Ted Patrick

    No problem, glad I could help. There is nothing like a good solid example. ;)

    EventDispatcher is very fast and doesn't have a big performance hit to speak of. ED is build into the V2 framework and is the prefered way to manage events in the player. It is very flexible as you can subscribe a listener or a function directly to a particular event.

    addEventListener("change", listener)

    or

    addEventListener("change", function(){trace('Beep')})

    Cheers,
    Ted ;)  

  3. # Anonymous Anonymous

    hmm, actually, i'm trying to compile your code, and i'm getting no error nor traced statement. reading the code makes sense, but in action, i'm not getting anything.
    i did notice that 'watch' in this line >>
    watch( 'theObserved' , function....
    is not syntax highlighted as blue in the actions panel. it should be, right?  

  4. # Anonymous Anonymous

    should it be observer.watch(... instead?

    (btw, sorry for the multiple posts)  

  5. # Blogger Ted Patrick

    The HTML color coding was mucking up the code. I modified the entry, just refresh the page. If you can't get the new formatless code to paste, try the FLA above.

    Last time I use that AS formatter.

    Cheers,

    Ted ;)  

  6. # Anonymous Anonymous

    oh i got it now, there was a piece of the code that i didn't copy right (had to set the line breaks myself).

    i just downloaded the .fla and it works fine. that's how i found my error, actually.

    as for the code highlighting, it is way easier to read the color-coded formatting, i'd stick with it if i were you. just wasn't sure if that unrecognized event declaration was my problem or not...

    thanks again man, this is great :)  

  7. # Blogger Ted Patrick

    I updated it yet again and it looks better now. When just the code is bold it looks good enough. I would much rather have code that was easily executable than code that is easy to read. If you can't get an example working, what is the point. I am not in the business of creating work for others. Plus this is good practice for a larger project shipping later this year, ASBE.

    Enjoy,

    Ted ;)  

  8. # Anonymous Anonymous

    ASBE? Oh do tell! Is that Flash, Central, or Flex-based?  

  9. # Blogger Ted Patrick

    ASBE - No spoiling a good suprize. Trust me it will be worth the wait. Ted ;)  

  10. # Anonymous Anonymous

    This post has been removed by a blog administrator.  

Post a Comment



© 2008 Ted On Flash