Hmmm....

>From your explanation, I think either I don't understand what happens in AS,
or you're misunderstanding the use of the singleton pattern.

In an attempt to understand better, here's an example of where I would use
the singleton pattern.

I have a requirement to talk to a socket on a server, but I need to make
sure that I only ever have a single connection from each user.

So...

I create a ServerConnection class that has something like this (ignoring
private constructors etc.):

ServerConnection {

private static instance:ServerConnection;

static function getInstance() {
if (this.instance == null) {
instance = new ServerConnection(url,port);
} else {
// make sure that the port and url for the instance match the passed url and
port.
// if not, barf an error
}
return instance;
}

public function getData() {
// Simple function that gets something from the server.
}
}

ok, so I have now guaranteed that I will only ever create a single
connection to the server because I'm always getting the same object back
regardless of how many times I call getInstance() in my code.

So I can happily do this anywhere I like in my code:

ServerConnection.getInstance().getData()

If I had something like what you have in the Math class, the getData()
method would have to be declared as static and it would have to check every
time you called it to see if there was an active connnection to the server.
If not it would have to create one and you would have to hope that 2 method
calls didn't step on each other and create multiple server connections.

Not sure if that clarifies or confuses, but hopefully you can let me know if
and how it differs in Flash.

Spike

On 10/28/05, JesterXL <[EMAIL PROTECTED]> wrote:
>
> ActionScript 2, no, no difference. You actually have to do a tincy bit of
> extra work to get AS2 to support getInstance like I've seen it in Java.
>
> This all goes way in AS3 since prototype is strictly in the hands of
> flash.util.Proxy; basically, prototype is now read-only, and Proxy is the
> only one who can wriggle around the rules in the new AVM.
>
> That's the one thing that always pissed me off about Cairngorm, and I
> debated for days on the ARP Advisory list till I was overulled merely by
> strength of numbers of opposing viewpoints.
>
> First, to clarify, when I speak of Singleton.getInstance(), I speak of the
> only way to utilize the Singleton's methods, so yes, if you want to call
> it
> calling a method on an instance, that's fine, but it's still spoken of as
> a
> Singleton only has 1 instance.
>
> That being the case, what I used to like to do was, even from AS1:
>
> class MyClass
> {
> public static function sup()
> {
> trace("yo");
> }
> }
>
> Which boils down to:
>
> function MyClass()
> {
> }
>
> MyClass.sup = function()
> {
> trace("yo");
> };
>
> Therefore, allowing this:
>
> MyClass.sup();
>
> Since Functions are objects in ActionScript 1 & 2, and about the same in
> 3.
>
> However, when talking about why EventDispatcher in Cairngorm, and
> Controller
> in ARP both have the getInstance method, I was told that it was to ensure
> that there was only ever 1 instance of it.
>
> I replied that there is; it's already defined, use it. Math.abs() works
> just fine, you don't do Math.getInstance().abs(), so why should I have to
> do
> that extra function when I know there is only 1 instance, and it's static,
> and ready to go?
>
> Fast-forward 4 days, class based vs. protoytpe languages, and demographics
> exploration later, and I see their point; if you don't understand how
> ActionScript is written, doing things like Math.random() really doesn't
> make
> any sense to traditional programmers, and having to know that much about
> how
> a language works is really unfair, doesn't make people want to dive in
> without it being familiar & comfortable, and is an elitist stance.
>
> So, although I think getIstance() isn't technically needed (in AS1 or
> AS2),
> I still see the need for it, and respect it from a traditional developer
> standpoint.
>
> ----- Original Message -----
> From: "Spike" <[EMAIL PROTECTED]>
> To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
> Sent: Friday, October 28, 2005 6:38 PM
> Subject: Re: [Flashcoders] Newbie AS3 question
>
>
> *snip*
> AS3 and Flex both hammer the point that this really has little use other
> than confirming for those programmers who are not familiar with
> ActionScript. Same goes for the Singleton.method vs.
> Singleton.getInstance().method argument; the latter is for those
> programmers
> who don't know ActionScript well.
> *snip*
>
> Maybe there's something I don't understand, or maybe you typed it wrong,
> but
> in every other OO language I've dealt with there's a significant but
> subtle
> difference between
>
> Singleton.getInstance().method()
>
> and
>
> Singleton.method()
>
> The former is calling an instance method and the latter is calling a
> static
> method.
>
> Instance methods have access to instance data that is persisted as long as
> the instance exists, static methods have access only to static data and
> that
> data passed in to the method call.
>
> Is this not true in ActionScript?
>
> Spike
>
> On 10/28/05, JesterXL <[EMAIL PROTECTED]> wrote:
> >
> > I'm the opposite end of the spectrum. This:
> >
> > class Box extends UIObject
> > {
> > function doStuff()
> > {
> > move(x + 10, y + 10);
> > setSize(width + 100, height + 100);
> > visible = !visible;
> > }
> > }
> >
> > looks more readable to me than:
> >
> > class Box extends UIObject
> > {
> > function doStuff()
> > {
> > this.move(this.x + 10, this.y + 10);
> > this.setSize(this.width + 100, this.height + 100);
> > this.visible = !this.visible;
> > }
> > }
> >
> > To each their own. I can see it justified in extending intrinsic
> classes,
> > as the first parameter to setInterval, and the first parameter in
> > Delegate.
> >
> > AS3 and Flex both hammer the point that this really has little use other
> > than confirming for those programmers who are not familiar with
> > ActionScript. Same goes for the Singleton.method vs.
> > Singleton.getInstance().method argument; the latter is for those
> > programmers
> > who don't know ActionScript well.
> >
> > If you do it every day, there is no point.
> >
> > ----- Original Message -----
> > From: "Muzak" <[EMAIL PROTECTED]>
> > To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
> > Sent: Friday, October 28, 2005 5:37 PM
> > Subject: Re: [Flashcoders] Newbie AS3 question
> >
> >
> > Well, to me it's the other way around.
> > Code that doesn't use proper references looks messy to me.
> >
> > Whe I'm lazy or in a hurry, I do skip them, but I usually find myself
> > adding
> > them afterwards anyway.
> >
> > So, I'm with ryanm on this one ;-)
> >
> > regards,
> > Muzak
> >
> > ----- Original Message -----
> > From: "Martin Wood" <[EMAIL PROTECTED]>
> > To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
> > Sent: Friday, October 28, 2005 11:03 PM
> > Subject: Re: [Flashcoders] Newbie AS3 question
> >
> >
> > >
> > >
> > > ryanm wrote:
> > >>> What I don't get is why it needs "this.addChild" instead of just
> > >>> addChild. I've been sick of the keyword "this" for a long time
> > >>> and have since avoided it in AS2.
> > >>>
> > >>> Any reason that it needs to be back in for AS3?
> > >>>
> > >> Maybe because it's one of the most useful scope references ever
> > >> invented?
> > >>
> > >> The fundamental concept that you seem to miss is that "addChild" is
> > >> meaningless by itself, it is a method of an object (in
> > >> proper OOP development), and if you just say "addChild", who is
> adding
> > >> the child?
> > >
> > > the context is the current class. Occasionally 'this' is useful if you
> > > happen to name a method parameter or local variable the
> > > same as a member variable and need to distinguish the two.
> > >
> > > But, I dont agree that its bad form to leave it out, nor is it any
> more
> > > difficult to maintain.
> > >
> > > in my opinion putting 'this' in everywhere to me just makes things
> > harder
> > > to read.
> > >
> > > thanks,
> > >
> > > Martin
> >
> >
> > _______________________________________________
> > Flashcoders mailing list
> > Flashcoders@chattyfig.figleaf.com
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > _______________________________________________
> > Flashcoders mailing list
> > Flashcoders@chattyfig.figleaf.com
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
>
>
>
> --
> --------------------------------------------
> Stephen Milligan
> Do you do the Badger?
> http://www.yellowbadger.com
>
> Do you cfeclipse? http://www.cfeclipse.org
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>



--
--------------------------------------------
Stephen Milligan
Do you do the Badger?
http://www.yellowbadger.com

Do you cfeclipse? http://www.cfeclipse.org
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to