5 Replies Latest reply on Jul 27, 2009 10:05 AM by Flex harUI

    Observer Pattern between Modules

    hockeyragazzo

      Hello,

       

      I'm trying to implement the observer pattern in a multi-modular application. This is being done so custom events can be fired when something changes in one module. This event is then listened for by all other modules. If the broadcasting module's id is in listening module's subscription list, the listening module needs to react by calling a function, effectively creating interchangeable intermodule communication.

       

      That all sounds nice in theory, but in practice I can't get the events to broadcast and be received properly. Does anyone know something about the Flex 3 event flow that I'm drastically overlooking? I know it's possible for this basic idea to work, but I'm missing something.

       

      I've attached a mock-up of what I'm trying to do as a Flex archive. Here there is a sender module and a receiver module. The text from the sender module should end up in the listener module's textarea, but it is not. I'm getting extremely desperate and any help would be greatly appreciated.

        • 1. Re: Observer Pattern between Modules
          Flex harUI Adobe Employee

          I don't have time to look at your code, but event flow never goes "sideways".  You can read the spec behind this implementation here: http://www.w3.org/TR/DOM-Level-3-Events/events.html

           

          In general, your choices are to bubble events from within the module and listen at the module (which many like to do, but I discourage), listen in capture phase at the module, or dispatch from some other known place like a Singleton

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Observer Pattern between Modules
            hockeyragazzo Level 1

            Thanks for taking the time to address my question, Alex. I spent the better part of last night trying to figure out how to create and reference a singleton using a model similar to this one:

             

            http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html

             

            Unfortunately, when I implemented this by calling the getInstance() method in each module, there did not appear to be a unique variable referencing the same instance of my class, but two distinct instances.

             

            I think I now understand how events flow in a Flex application. I have a few questions for the community if anybody is willing to put up with my ignorance

             

            If I dispatch an event from within a Module loaded in a ModuleLoader, will the Application pick up that event? Do I need to set bubbling=true in order for this to occur?

             

            If so, is it possible to rebroadcast that event from the Application to all children below it?

             

            Alternatively, if I were to use a singleton, how can I reference the exact same instance of a class from multiple modules so that I can use that class as a sort of broadcaster/receiver that modules can use to send and get information about events?

            • 3. Re: Observer Pattern between Modules
              Flex harUI Adobe Employee

              If you dispatch an event from a Module, you can always see it in capture phase from the Application, and in bubble phase if you set bubble=true on the event, but like I said earlier, I discourage bubbling.

               

              There is no API to broadcast "down" the tree.  Hence the Singleton Pattern.  All interested parties listen to the singleton.  It is essentially a global that you subscribe to if you're interested.

               

              With Modules, the singleton pattern requires global registration.  Flex provides the mx.core.Singleton class where you register an instance of the implementation by interface.  The interface needs to be in the main application or injected into the main applicationDomain via a shared-code module.  You may also simply put the class Skinner singleton in the main application.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Observer Pattern between Modules
                hockeyragazzo Level 1

                Hi Alex,

                 

                Thanks for your help so far. You have helped me better understand the Flex event model.

                 

                I've been trying to figure out how to use an interface with the Singleton class, however, there isn't very much documentation on the mx.core.Singleton class. If I understand you correctly, this is what I need to do:

                 

                1. Convert my custom event manager class into an interface

                2. Use the Singleton class to create a single instance of this class at the Application level at creationComplete

                3. Refer to this instance in each module by registering a variable with the location Application.application.myManagerInstance

                4. Add listeners and dispatch events within modules from this registered variable (or dispatch events with bubbling on from within the module)

                 

                I don't think I will have any trouble with 1, 3, and 4, but I don't know about 2. Considering the apparent absence of docs on the subject, could you please direct me to some info (or give me a quick rundown) on instantiating interfaces globally through the Singleton class? Your help continues to be greatly appreciated.

                 

                Thanks again

                • 5. Re: Observer Pattern between Modules
                  Flex harUI Adobe Employee

                  If you scan our source code for "Singleton", the pattern will hopefully become apparent.  You can look at ResourceManager and/or DragManager as a start.  The classes implement an interface and you register an instance of that class at startup and other code requests that interface from Singleton when needed

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui