Thanks David and Muzak,

What I've done is used an existing Broadcaster.as class to create events, and then created a pointer in the PageController + relatedListening classes to the Broadcaster so that they can register eventListen + DispatchEvent. I found this conceptually easier to implement than:

.addEventListener("click", Delegate.create(this, function));

But I think this also has to do with my unfamiliarity with using this design approach... especially in how to make two classes talk to each other [all the examples I find have the Delegate function call something within the same class; but i need the events to go between classes...?]

So here is how I am doing it... the Broadcaster class:

//THE BROADCASTER
class com.oop.core.Broadcaster {

        private var __listeners:Array;

        function Broadcaster() {
                __listeners = [];
        }

public function addEventListener(evnt:String, lstnr:Object, mappedTo:String):Boolean {
                var ev:String;
                var li:Object;
                for (var i in __listeners) {
                        ev = __listeners[i].event;
                        li = __listeners[i].listener;
                        if (ev == evnt && li == lstnr) return false;
                }
                __listeners.push({event:evnt, listener:lstnr, 
mappedTo:mappedTo});
                return true;
        }

        public function dispatchEvent(evnt:String, params:Object):Void {
                var evtObj = {type:evnt, parameters:params};
                for (var i:String in __listeners) {
                        if (__listeners[i].event == evnt) {
                                __listeners[i].listener[evnt](evtObj);

__listeners[i].listener[__listeners[i].mappedTo](evtObj);
                        }
                }
                this[evnt + "Handler"](evtObj);
        }

        public function removeEventListener(evnt:String, lstnr:Object):Boolean {
                var ev:String;
                var li:Object;
                for (var i = 0; i < __listeners.length; i++) {
                        ev = __listeners[i].event;
                        li = __listeners[i].listener;
                        if (ev == evnt && li == lstnr) {
                                __listeners.splice(i, 1);
                                return true;
                        }
                }
                return false;
        }

}

And then in each class I want to register to the broadcaster I write:

//THE DISPATCHER
class com.blabla.PageController {
        
        //init pointers:
        private var myModel:Object;
        private var myBroadcaster:Object;
        private var myURLMapper:Object;
        //init window state:
        private var activeWindow:Boolean;
        
public function PageController (__myModel:Object,__myBroadcaster:Object,__myURLMapper:Object) {
                myModel = __myModel;
                myBroadcaster = __myBroadcaster;
                myURLMapper = __myURLMapper;
                activeWindow = false;
        }
        
        public function urlChange (__newURL:String):Void {
                if ((__newURL == "") || (__newURL == "/")) {
                        activeWindow = false;
                } else {
                        activeWindow = true;
                }
myBroadcaster.dispatchEvent("newURL",{myURL:__newURL,activate:activeWindow,relatedXMLfile:myURLMapper.matchURL(__newURL)});
        }
        
}

//THE EVENT LISTENER
class com.blabla.StatsCollector {
        var myBroadcaster:Object;
        
        public function StatsCollector (__myBroadcaster) {
                myBroadcaster = __myBroadcaster;
                myBroadcaster.addEventListener("newURL",this)
        }
        
        public function newURL (__newURL) {
                trace ("StatsCollector = " + __newURL.parameters.myURL);
        }
}

Are there any advantages/disadvantages to the way I am setting up my listeners/dispatchers in this way vs. addEventListener("event", Delegate.create(this, function));?

I want to make sure I'm doing it right as I will soon be making loads of listeners/dispatchers...

thanks again!!!

seb.

David Ngo wrote:
IMO, EventDispatcher will work well in most cases. If you need a more
flexible or scalable way to handle events, then I'd suggest writing your own
event framework based on the Observer pattern as there are some limitations
to EventDispatcher.



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of sebastian
chedal
Sent: Friday, May 04, 2007 5:24 AM
To: flashcoders@chattyfig.figleaf.com
Subject: [Flashcoders] Re: AS2: Design Pattern: event based or pointer?
frommodel or controller?

Alo,

So I've done some more independant research,

I think I'm going to use an EventDispatcher system from the PageController:

http://www.adobe.com/devnet/flash/articles/creating_events.html

Instead of pointers. As I think this makes more sense.
Ofcourse, any insight from the 'pros' still welcome!

With kind,

Seb.

On 5/3/07, sebastian <[EMAIL PROTECTED]> wrote:
Hello folks,

My next question is conceptual...

I've made good progress on many of the classes in my application, but
I'm now caught in a dilemma regarding approach.

As mentioned in a previous mail, I'm building a system that can be
divided into two: 1 part does general world 3D animation until the pages
are opened; this was relatively easy to make... the second part of the
system deals with generating pages [its a templating MVC system]; this
is the tough part!

At the center of the application is my class: PageController

Linked to it are 3 other Classes: PageModel [to load new pages],
StatsCollector [to record new pages called] and MouseController [told to
turn off 3D motion when pages are opened].

I was planning on using an object-instance pointer in the PageController
formed during its construction, to tell the associated Classes that a
new event has ocured, but someone pointed out to me that I should be
using an event based system instead [so that's its easier to add new
Classes that listen to the Controller in the future]

Here is what I have:

class com.blabla.PageController {
        //Object pointers
        private var pageModel:Object;
        private var statsCollector:Object;
        private var mouseController:Object;
        private var urlMapper:Object;
        //Page open/close status
        private var activeWindow:Boolean;

        function PageController (
                __pageModel:Object,
                __mouseController:Object,
                __statsCollector:Object,
                __urlMapper:Object)
                {
                //Pointers:
                this.pageModel = __pageModel;
                this.statsCollector = __statsCollector;
                this.mouseController = __mouseController;
                this.urlMapper = __urlMapper;
                //Set initial state:
                this.activeWindow = false;
        }

        //this method is called by SWFAddress on url changes:
        public function urlChange(__name:String):Void {

                // MOUSE CONTROLLER
                //set the page active true to turn off
                //3D mouse system:

                this.activeWindow = true;

                //tell 3D mouse system to check status:
                this.mouseController.checkWindowStatus();

                // PAGE MODEL
                //call URL mapper returns an XML file based
                //on the url __name
                //this XML file is then passed to the pageModel
                //which will then notify the PageView of changes

                this.pageModel.buildPage (this.urlMapper.convertLink
(__name));

                // STATS COLLECTOR
                //run the method in 'statscol' with url:

                this.statsCollector.callURL(__name);

        }

}

So, my question is... is this the right way to be sending events? By
making pointers and calling methods in connected classes? Also, should I
store my "activeWindow" parameter in the Controller? I have a feeling
its in the wrong Class; but from a Code-clarity perspective it does make
sense to have it here as the controller is a state-dispatcher...?

Or should I be using an event based system instead of pointers? If so,
could someone lightly outline what the difference would be like in such
a set up? I'm not very familiar with using events to drive app like
this. I'd be very thankful.

Here is the latest diagram of the system:
http://www.chedal.org/temp/uml_design_03.png
[this that are blue/green are things I've built until now]

Note I chose to connect the MouseController and StatsCollector to the
PageController instead of the PageModel because if I did so the
PageModel would get very long in terms of code; I want the PageModel to
focus JUST on storing the Templates' data that the View will build [by
refering to the PageModel's loaded data]. Also, if I linked everything
to the PageModel instead, the functionality of the PageController as a
dispatcher would be made redundant... am I right in this thinking?

If you wish I can copy-paste the code of other Classes [assuming I've
written the code!], but I don't think this is necessary to answer my
question.

With kindness,

Sebastian.

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to