View Full Version : [AS3] Of what value be these namespaces?
maskedMan
06-20-2008, 06:30 PM
I've recently acquired the Essential Actionscript 3.0 book and have been reading straight through in my spare time and on the bus. I'm currently about halfway through the chapter on Namepsaces; I have to admit that it's making my head hurt. I can understand the potential value of using a URL string to determine the country of origin of a site's visitor, but in other instances, I'm just not seeing it. It seems to me so far like it's basically a convoluted, roundabout way to control access to variable and function names.
I'm not seeing the value here. Can anyone give me some examples where the use of namespaces made their lives appreciably easier than had they done otherwise? Are there specific best practices involving them? If so, what makes them best practices aside from convention?
creynders
06-21-2008, 10:08 AM
I'm certainly no expert on namespaces and don't know if this is what they are really meant for, but I use them for 2 kinds of things:
1. as qualifiers for event listeners
2. for implementations of state machines
Both can be done in lots of different (and maybe better ??) ways.
I'll give an example of each
private namespace radioButtonNS;
private namespace simpleButtonNS;
this.addEventListener( someRadioButton, radioButtonNS::onClick );
this.addEventListener( someSimpleButton, simpleButtonNS::onClick );
radioButtonNS function onClick( event : Event ) : void{
// do something
}
simpleButtonNS function onClick( event : Event ) : void{
//do something
}
To me, this is a very clear way to separate the event listeners from the "normal" functions and from each other. Of course, each case has different needs or can be constructed more easily; sometimes I create one event handler which uses a switch statement.
But to me the power of namespaces is more in the second usage.
Let's say we have an button which in all states it reacts to the same events, but not in the same way.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class SomeButton extends MovieClip
{
private namespace deselectedState;
private namespace selectedState;
private var state : Namespace;
public function SomeButton()
{
this.addEventListener( MouseEvent.MOUSE_OVER, eventHandler );
this.addEventListener( MouseEvent.MOUSE_OUT, eventHandler );
this.addEventListener( MouseEvent.CLICK, eventHandler );
state = deselectedState;
}
private function eventHandler( event : MouseEvent ) : void{
switch( event.type ){
case MouseEvent.CLICK:
state::onClick();
break;
case MouseEvent.MOUSE_OUT:
state::onMouseOut();
break;
case MouseEvent.MOUSE_OVER:
state::onMouseOver();
break;
}
}
selectedState function click( event : MouseEvent ) : void{
state = deselectedState;
}
selectedState function mouseOver( event : MouseEvent ) : void{
//set color red
}
selectedState function mouseOut( event : MouseEvent ) : void{
//set color orange
}
deselectedState function click( event : MouseEvent ) : void{
state = selected;
}
deselectedState function mouseOver( event : MouseEvent ) : void{
//set color cyan
}
deselectedState function mouseOut( event : MouseEvent ) : void{
//set color blue
}
}
}
Of course, this is just an example, this particular case I'd implement differently, since it's such an easy case.
maskedMan
06-21-2008, 04:38 PM
Thank you for the example. It seems sensible, and a lot more applicable to what I'm used to making than the KidsGame samples from the book where various classes define their own namespaces to (seemingly needlessly?) differentiate their similarly named methods.
Assertnfailure
07-03-2008, 01:36 AM
State machine is a good example....
I basically use namespaces on properties/methods whenever I want a level of restriction comparable to internal, without the necessity of keeping the code all in the same package.
I wouldn't waste too much time on namespaces for things like event handlers, because really those should just be private inside the class anyway. Instead of...
radioButtonNS function onClick(evt:Event):void{}
simpleButtonNS function onClick(evt:Event):void{}
I'd use...
private function radioButtonClickHandler(evt:Event):void{}
private function simpleButtonClickHandler(evt:Event):void{}
Also, namespaces can used for xml nodes too.
NickZA
12-13-2008, 07:48 PM
ZOMG. This is what I have been looking for.
(in 1960's household advert narration style) "At last, I can arrange my packages they way I feel it's logical to do, without concerns about how that affects internal access! Thank you, ASSERTNFAILURE!"
You are the man/woman/androgyne, delete as appropriate. :cool:
srivello
06-01-2009, 09:37 AM
Great post. I love the idea of assisting developers so they only seeing the methods via intellisense (of Flex Builder) for methods they need to see. I created a new demo to show that off. http://www.blog.rivello.org/?p=422
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.