Ted Patrick - Demos & MAX @ Adobe Systems


Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



In-App Messaging with Tubes

DIGG IT!     10 Comments Published Friday, August 07, 2009 at 9:43 PM .

One issue that has always troubled me is that within class based apps communication between class instances isn't simple. I want a simple system, where you can send a single message to a named instance or broadcast a message to all listeners. Also within the message I want to send an object allowing data to be routed around the application. Today I wrote a class called Tubes that does just that.

com.diverted.messaging.Tubes

Lets take a look:

//import the class
import com.diverted.messaging.Tubes;

//to start receiving messages, simply add a name and a method
Tubes.add( "root" , this.tube );

//send an object to all receivers
Tubes.send( { type:"paymentRecieved" , amount:34.52 } );

//send an object to particular instance
Tubes.sendTo( "accountant" , { type:"paymentRecieved" , amount:34.52 } );

//to stop receiving messages
Tubes.remove( "root" );

I like the simplicity of Tubes because once you import the class, you can message within the application easily. The method passed into Tubes.add is called with a single object argument so you can structure the object as needed.

Cheers,

Ted :)

10 Responses to “In-App Messaging with Tubes”

  1. # Anonymous Anonymous

    Nice, very nice. I like that a lot.  

  2. # OpenID id

    Nice work, Ted. At first, I was thinking PureMVC could solve your problem, but it falls short of addressing specific named instances.

    Again, nice work :-)  

  3. # Blogger Jens

    Wouldn't it make sense to change listeners:Object = {}; into listeners:Dictionary = new Dictionary(true);? That way you could use the actual objects as keys and you wouldn't have to rely on strings for naming the objects. The add method could be simplified to only have one argument, the object you want to add as a listener. Or am I missing something?  

  4. # Anonymous fredo

    isn't it bad OOP to allow anything to talk to anything using only string identifiers?  

  5. # Blogger Ted Patrick

    Jens, that is a great point. The issue surfaces when you want to send a message and do not have access to an instance. Take email, I do not have to have you present to send you a message by your email address. Strings are also handy because you can make paths easily via dot notation:

    Tubes.add( "root.tube" , this.tube );
    Tubes.add( "root.tube.click" , this.tubeClick );
    DataTubes.add( "root.tube.loadData" , this.onLoadData );
    DataTubes.loadData ( { url:'http://yahoo.com', name:'root.tube.loadData'}

    I did explore Dictionary for perf reasons but in tests found it to be a non-issue.

    Ted :)  

  6. # Blogger Ted Patrick

    Bad OOP? What is that?

    The goal is to simply allow two separate classes to route objects to one another or route message to many objects via simple api.

    I think bad OOP is passing references around so you can talk to other objects. The leads to loosing track of references and bloated GC in apps.

    My 2 cents,

    Ted :)  

  7. # Anonymous Anonymous

    Hmmmm, I must not get it. Isn't this basic event model? Can't you do this with Cairngorm event dispatcher, an event and anyone that implements the interface?  

  8. # Blogger Ted Patrick

    It is about the simplest callback event model you can make with AS3. If one class wants to let another class know something happened, you just send a message. Also there are no framework dependencies as this can be used in AS3 or Flex without issue.

    Ted :)  

  9. # Blogger Ben Garney

    Great idea, Ted. Not suitable for every case, but definitely a very powerful technique.

    IMHO stuff like this has a lot more value than a complex MVC framework.  

  10. # Blogger Eerk

    Great class! But what's the difference between tubes and just broadcasting an event? (and having another class listen to that event?)  

Post a Comment

Where to find me:

Ted on Twitter - @AdobeTed
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe


Latest

Lists

Links

Jobs

Flex Jobs
city, state, zip

Archives