Can you elaborate? Why wouldn't the static class work in that case? ----- Original Message ----- From: "Spike" <[EMAIL PROTECTED]> To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com> Sent: Friday, October 28, 2005 9:54 PM Subject: Re: [Flashcoders] Newbie AS3 question
ok, That's just a static class. Like I said, there's a subtle but important difference between singleton and a static class. Here's another example. You have a requirement to provide a currency formatter. One way to do this is to create a singleton that returns a different currency formatter depending on which locale you are in. So in the class you would have something like this (Omitting method declarations for simplicty): public class CurrencyFormatter { class USCurrencyFormatter extends CurrencyFormatter { } class UKCurrencyFormatter extends CurrencyFormatter { } } Now if I call CurrencyFormatter.getInstance() it gives me the correct formatter for my locale. With your static class approach you have to check in every method what the locale is and handle it accordingly. That's fine for one or two locales, but if you want to handle 20, it gets pretty ugly. You can solve the problem in other ways of course, but it does demonstrate the difference between static classes and the singleton pattern. The singleton pattern offers you a lot more possibilities. Spike On 10/29/05, JesterXL <[EMAIL PROTECTED]> wrote: > > To clarify: > > > class ServerConnection > { > private static var url; > private static var port; > private static var socket; > > public static function connect(p_url, p_port) > { > url = p_url; > port = p_port; > socket = new Socket(); > socket.connect(url, port); > } > > public static function getData() > { > // Simple function that gets something from the server. > } > } > > Then to use: > > ServerConnection.connect(myURL, myPort); > > ----- Original Message ----- > From: "JesterXL" <[EMAIL PROTECTED]> > To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com> > Sent: Friday, October 28, 2005 9:25 PM > Subject: Re: [Flashcoders] Newbie AS3 question > > > Naw, I don't know the exact way Singleton is implemented, hence my long > battle with finding clarification. It could of been solved in 10 seconds > over a beer, but email sux. > > I figured Math.abs was the Singleton pattern, and ARP, me, Sho, Steven > Webster, and everyone else apparently has their own version of Controller > as > well. I figured I knew enough to use it. > > For instance, your example makes perfect sense and I can see why you'd > want > to do it that way. I, on the other hand would just put an if then > statement > in the connect method or whatever to ensure we're connected before making > a > method call, else throw an exception. > > Are they both the Singleton pattern? Does it matter? > > > ----- Original Message ----- > From: "Spike" <[EMAIL PROTECTED]> > To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com> > Sent: Friday, October 28, 2005 7:14 PM > Subject: Re: [Flashcoders] Newbie AS3 question > > > 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 > > _______________________________________________ > 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