3

I'm building a flash app (just AS3 with FlashDevelop) and I'm having some trouble keeping things loosely coupled around the event system. I've done a lot of reading about central event systems and static eventdispatchers but they don't quite fit the bill for me.

What I'm building is similar to a video player. I have a Player class which is the parent of all the other little parts of the app. The Player class extends Sprite and I've currently designed it so that you could instantiate multiple Player's and put them on the stage. I also have a Controller class which extends EventDispatcher and I dispatch all my events through this class. It's a central event class.

The problem is that I need to pass around a reference to this class so that all the other classes can dispatch and listen through it. Passing around the reference works but it's the exact opposite of loose coupling. I know I could make a static EventDispatcher that all the classes could see but then if I had two or three Player's on the stage they would all hear each others events.

How can I create a type of sandbox that will allow all the child classes of a Player instance to be aware of the central dispatcher without passing a reference or making it static?

Ryan Bosinger
  • 1,832
  • 2
  • 16
  • 25
  • You could use injection instead of passing around a reference 'manually'. – RIAstar Aug 22 '11 at 23:10
  • BTW I'm not sure it's a very good idea to use a 'central dispatcher' in a custom component. I can see the benefit in a large scale application where one module must talk to the other and you don't want to bubble through the entire displaylist. But for something as simple as a video player I fail to see the point. – RIAstar Aug 22 '11 at 23:19
  • Well it's more complicated than just a video player but I didn't want to elaborate. What do you mean by injecting? Let's say the Controller belongs to the Player but I want a far off UI piece to listen to Controller (ex: sidebar.widgets.notemaker). Do I need to pass the instance of Controller to Sidebar, Widgets, and then Notemaker? – Ryan Bosinger Aug 22 '11 at 23:36
  • 1
    By 'injecting' I mean: have an 'Inversion of Control'-framework (IoC) do the passing around for you. If only NoteMaker requires a dispatcher, neither Sidebar nor Widgets will have to know about it. The IoC will scan/watch the displaylist and 'inject' your dispatcher wherever it finds a property that meets the right conditions. Some frameworks that include an IoC are RobotLegs and Parsley but there's many more. – RIAstar Aug 22 '11 at 23:47
  • Come to think about it, injection will probably not be your best bet either. You'll run into the same problem as with the static dispatcher: you need a separate instance of Controller for each chain in the displaylist. It can probably be configured with some of the IoC-frameworks, but it feels so clunky. What's so wrong about bubbling? – RIAstar Aug 23 '11 at 00:10
  • The problem with bubbling is that not every class is in the displaylist. – Ryan Bosinger Aug 23 '11 at 00:41

2 Answers2

0

It turns out what I was really trying to understand was loose-coupling using something like dependency injection.

I never ended up doing this in any AS3 projects as I don't do much of it anyway. Having done more C# recently I more easily grasped this concept using libraries such as StructureMap and Ninject.

For AS3 you could use a framework like Robotlegs. This will probably change the way you code AS3 and may not be for all developers/situations.

Ryan Bosinger
  • 1,832
  • 2
  • 16
  • 25
0

I'd suggest to use my static dispatcher, this one has an ID mechanism that allows to tell which Player instance dispatches an event.

Community
  • 1
  • 1
www0z0k
  • 4,444
  • 3
  • 27
  • 32