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. Ted on Twitter - @AdobeTed
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe
Nice, very nice. I like that a lot.
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 :-)
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?
isn't it bad OOP to allow anything to talk to anything using only string identifiers?
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 :)
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 :)
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?
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 :)
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.
Great class! But what's the difference between tubes and just broadcasting an event? (and having another class listen to that event?)