[Flashcoders] Re: Re: Newbie AS3 question
Yes, but how do you know order of construction of static clases, if you have more each one init depends on other? JesterXL [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] If you utilize a static function, it would be called when the class itself is created in ActionScript, before frame 1 even runs: class Test { static var inited = InitThisTest(); } Why wouldn't that solve it? - Original Message - From: A.Cicak [EMAIL PROTECTED] To: flashcoders@chattyfig.figleaf.com Sent: Saturday, October 29, 2005 6:59 AM Subject: [Flashcoders] Re: Newbie AS3 question Problem with JasterXL's class is initialization. For example if you had to initialize something in that class you should make static method called InitCurrencyFormatter(). But when to call it? If other classes are using CurrencyFormatter than you should ensure to call InitCurrencyFormatter() before construction of any class which uses it is called. But what if InitCurrencyFormatter also depends on some other class which is made just with static methods (like JasterXL's). In that case that other class would have to have InitOtherClass() and it also should be called before other objects are constructed and before InitCurrencyFormatter. Now imagine 20 classes like that and thousands of lines of code depending on them, it would be almost impossible to keep track when to initialize any of these classes, and it would be very error prone (you change order of execution and you start using uninitialized objects). If you make these classes singletons you don't have that problem, because when you call getInstance() if object was not initialized getInstance will call new and that will call its constructor ( which now replaces InitCurrencyFormatter, InitOtherClass, etc..) and it will ensure that all objects are initialized at time of their use, and you do not have to worry about order of execution. Spike [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Sure, Here's a slightly more complete implementation of that last example: public class CurrencyFormatter { private static formatter:CurrencyFormatter; public function getInstance():CurrencyFormatter { // use ExternalInterface or IP lookup or whatever to determine locale if (locale == UK) { formatter = new UKCurrencyFormatter(); } else { formatter = new USCurrencyFormatter(); } return formatter; } class USCurrencyFormatter extends CurrencyFormatter { public formatValue(val:Number) { // very simplistic formatting return $ + String(val); } } class UKCurrencyFormatter extends CurrencyFormatter { public formatValue(val:Number) { // very simplistic formatting return £ + String(val); } } } Let me know if that explains it a bit better. Spike On 10/29/05, JesterXL [EMAIL PROTECTED] wrote: 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
Re: [Flashcoders] Re: Re: Newbie AS3 question
The order is based on inheritance, or #initclip order, which is based on which classes is nested where. #initclip won't work, though, if you're class doesn't extend MovieClip, and if you don't extend something, you have to rely on the compiler. As such, you'll have to utilize an instance, at least via a depend variable inside the class: private var depend:OtherClassINeed; I can see what you mean, though, how that gives very little control, and isn't really flexible. - Original Message - From: A.Cicak [EMAIL PROTECTED] To: flashcoders@chattyfig.figleaf.com Sent: Saturday, October 29, 2005 5:31 PM Subject: [Flashcoders] Re: Re: Newbie AS3 question Yes, but how do you know order of construction of static clases, if you have more each one init depends on other? JesterXL [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] If you utilize a static function, it would be called when the class itself is created in ActionScript, before frame 1 even runs: class Test { static var inited = InitThisTest(); } Why wouldn't that solve it? - Original Message - From: A.Cicak [EMAIL PROTECTED] To: flashcoders@chattyfig.figleaf.com Sent: Saturday, October 29, 2005 6:59 AM Subject: [Flashcoders] Re: Newbie AS3 question Problem with JasterXL's class is initialization. For example if you had to initialize something in that class you should make static method called InitCurrencyFormatter(). But when to call it? If other classes are using CurrencyFormatter than you should ensure to call InitCurrencyFormatter() before construction of any class which uses it is called. But what if InitCurrencyFormatter also depends on some other class which is made just with static methods (like JasterXL's). In that case that other class would have to have InitOtherClass() and it also should be called before other objects are constructed and before InitCurrencyFormatter. Now imagine 20 classes like that and thousands of lines of code depending on them, it would be almost impossible to keep track when to initialize any of these classes, and it would be very error prone (you change order of execution and you start using uninitialized objects). If you make these classes singletons you don't have that problem, because when you call getInstance() if object was not initialized getInstance will call new and that will call its constructor ( which now replaces InitCurrencyFormatter, InitOtherClass, etc..) and it will ensure that all objects are initialized at time of their use, and you do not have to worry about order of execution. Spike [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Sure, Here's a slightly more complete implementation of that last example: public class CurrencyFormatter { private static formatter:CurrencyFormatter; public function getInstance():CurrencyFormatter { // use ExternalInterface or IP lookup or whatever to determine locale if (locale == UK) { formatter = new UKCurrencyFormatter(); } else { formatter = new USCurrencyFormatter(); } return formatter; } class USCurrencyFormatter extends CurrencyFormatter { public formatValue(val:Number) { // very simplistic formatting return $ + String(val); } } class UKCurrencyFormatter extends CurrencyFormatter { public formatValue(val:Number) { // very simplistic formatting return £ + String(val); } } } Let me know if that explains it a bit better. Spike On 10/29/05, JesterXL [EMAIL PROTECTED] wrote: 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