RE: [flexcoders] Re: Singleton not usable?
Thanks Tim, I should not have been so brief in my original reply. I didn't mean to imply that a implementing a Singleton is not possible in ActionScript, only that you can't rely on the default access specifier to make your constructor private, as you can in C#. In ActionScript 3.0, the default access specifier for most class members is "internal". The constructor is the exception, and is "public" whether or not you include that keyword in the declaration. Francis > -Original Message- > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On > Behalf Of turbo_vb > Sent: Friday, April 14, 2006 11:57 PM > To: flexcoders@yahoogroups.com > Subject: [flexcoders] Re: Singleton not usable? > > This a modified version of Jeff Tapper's singleton DataManager > class for AS3 web service calls. Francis, note the private class > constuctor at the bottom. Sorry about the jagged alignment. > > - Tim Hoff > > > package code.business { > > import mx.managers.CursorManager; > import flash.events.EventDispatcher; > import mx.rpc.soap.WebService; > import mx.rpc.events.ResultEvent; > import mx.rpc.events.FaultEvent; > import mx.rpc.AbstractOperation; > import mx.controls.Alert; > import flash.util.*; > > public class DataServices extends EventDispatcher { > private var ws:WebService; > private static var instanceMap:Object = new Object(); > public function DataServices(pri:PrivateClass, wsdl:String) > { > this.ws = new WebService(); > ws.wsdl = wsdl; > ws.loadWSDL(); > ws.useProxy = false; > } > public static function getDataService > (wsdl:String):DataServices > { > if(DataServices.instanceMap[wsdl] == null) > { > DataServices.instanceMap[wsdl] = new DataServices > (new PrivateClass(),wsdl); > } > > var ds:DataServices = DataServices.instanceMap[wsdl]; > if(ds.ws.canLoadWSDL()) > { > return ds; > } else { > throw new Error("BAD WSDL:"+wsdl); > } > } > public function makeRemoteCall > (methodName:String,showBusyCursor:Boolean,args:Object):void > { > var op:mx.rpc.AbstractOperation = ws[methodName]; > if(showBusyCursor) > { > CursorManager.setBusyCursor(); > } > ws.addEventListener("result",onResult); > ws.addEventListener("fault",onFault); > if(args) > { > op.arguments = args; > } > op.send(); > > } > private function onResult(result:ResultEvent):void > { > CursorManager.removeBusyCursor(); > this.dispatchEvent(result); > } > private function onFault(fault:FaultEvent):void > { > CursorManager.removeBusyCursor(); > this.dispatchEvent(fault); > } > public override function toString():String > { > return "DataServices"; > } > } > } > > /** PrivateClass is used to make DataServices constructor private > */ > class PrivateClass > { > public function PrivateClass() {} > } > > > > > > --- In flexcoders@yahoogroups.com, "Francis Cheng" <[EMAIL PROTECTED]> > wrote: > > > > That works in C# because the default access specifier for class > members > > in C# is private. It won't work in ActionScript 3.0 because the > > constructor is always public, whether or not you declare it as > such. > > > > Francis > > > > > -Original Message- > > > From: flexcoders@yahoogroups.com > [mailto:[EMAIL PROTECTED] > > On > > > Behalf Of crnet_flex > > > Sent: Friday, April 14, 2006 3:19 PM > > > To: flexcoders@yahoogroups.com > > > Subject: [flexcoders] Re: Singleton not usable? > > > > > > > > > Hi > > > > > > Have anyone tried using a construction like the following? > > > > > > public class Singleton > > > { > > > public static const Instance:Singleton = new Singleton(); > > > > > > function Singleton() > > > { > > > } > > > > > > public function doSomething():void > > > { > > > } > > > } >
RE: [flexcoders] Re: Singleton not usable?
We've had other singleton discussions before so I won't re-hash, but will point out that we don't have the same thread/locking problem that Java does when accessing the singleton because your AS code is single-threaded. So doing the if (instance == null) instance = new... is just fine. Matt -Original Message- From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of crnet_flex Sent: Friday, April 14, 2006 3:19 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: Singleton not usable? --- In flexcoders@yahoogroups.com, "Sonja Duijvesteijn" <[EMAIL PROTECTED]> wrote: > > Hi all, > > I'm trying to make a singleton class as demonstrated below, but get the > error: "a constructor may only be declared 'public'". Well, that error is > clear, the reason for it is not however. > Isn't it possible anymore to make a singleton class, or is there another > method for it? > > Class Singleton { > > private static var _instance:Singleton = null; > > private function Sinleton() { > } > > public static function getInstance():Singleton { >if(Singleton._instance == null) { > _instance = new Singleton(); >} >return _instance; > } > > } > > Any help would be much appreciated. > > Kind regards, > Sonja Duijvesteijn > Hi Have anyone tried using a construction like the following? public class Singleton { public static const Instance:Singleton = new Singleton(); function Singleton() { } public function doSomething():void { } } This is actually the recommandations of creating a singleton in c#, and it seems that it fullfils the job here as well. I know that, if you use flexbuilder 2 beta, then you'll get a warning if you leave out the scope of the constructor, but maybe it's worth it. By the way, be aware that the singleton posted initially as an example is not a recommended way of creating a singleton, i guess it's not a problem in AS3, but lazy loaded singletons must have a syncronized/looked load section when thread safety is an issue. BR Casper -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com Yahoo! Groups Links -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/flexcoders/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
[flexcoders] Re: Singleton not usable?
This a modified version of Jeff Tapper's singleton DataManager class for AS3 web service calls. Francis, note the private class constuctor at the bottom. Sorry about the jagged alignment. - Tim Hoff package code.business { import mx.managers.CursorManager; import flash.events.EventDispatcher; import mx.rpc.soap.WebService; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.rpc.AbstractOperation; import mx.controls.Alert; import flash.util.*; public class DataServices extends EventDispatcher { private var ws:WebService; private static var instanceMap:Object = new Object(); public function DataServices(pri:PrivateClass, wsdl:String) { this.ws = new WebService(); ws.wsdl = wsdl; ws.loadWSDL(); ws.useProxy = false; } public static function getDataService (wsdl:String):DataServices { if(DataServices.instanceMap[wsdl] == null) { DataServices.instanceMap[wsdl] = new DataServices (new PrivateClass(),wsdl); } var ds:DataServices = DataServices.instanceMap[wsdl]; if(ds.ws.canLoadWSDL()) { return ds; } else { throw new Error("BAD WSDL:"+wsdl); } } public function makeRemoteCall (methodName:String,showBusyCursor:Boolean,args:Object):void { var op:mx.rpc.AbstractOperation = ws[methodName]; if(showBusyCursor) { CursorManager.setBusyCursor(); } ws.addEventListener("result",onResult); ws.addEventListener("fault",onFault); if(args) { op.arguments = args; } op.send(); } private function onResult(result:ResultEvent):void { CursorManager.removeBusyCursor(); this.dispatchEvent(result); } private function onFault(fault:FaultEvent):void { CursorManager.removeBusyCursor(); this.dispatchEvent(fault); } public override function toString():String { return "DataServices"; } } } /** PrivateClass is used to make DataServices constructor private */ class PrivateClass { public function PrivateClass() {} } --- In flexcoders@yahoogroups.com, "Francis Cheng" <[EMAIL PROTECTED]> wrote: > > That works in C# because the default access specifier for class members > in C# is private. It won't work in ActionScript 3.0 because the > constructor is always public, whether or not you declare it as such. > > Francis > > > -Original Message- > > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] > On > > Behalf Of crnet_flex > > Sent: Friday, April 14, 2006 3:19 PM > > To: flexcoders@yahoogroups.com > > Subject: [flexcoders] Re: Singleton not usable? > > > > > > Hi > > > > Have anyone tried using a construction like the following? > > > > public class Singleton > > { > > public static const Instance:Singleton = new Singleton(); > > > > function Singleton() > > { > > } > > > > public function doSomething():void > > { > > } > > } > > > > This is actually the recommandations of creating a singleton in c#, > > and it seems that it fullfils the job here as well. > > > > I know that, if you use flexbuilder 2 beta, then you'll get a > > warning if you leave out the scope of the constructor, but maybe > > it's worth it. > > > > By the way, be aware that the singleton posted initially as an > > example is not a recommended way of creating a singleton, i guess > > it's not a problem in AS3, but lazy loaded singletons must have a > > syncronized/looked load section when thread safety is an issue. > > > > BR Casper > -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/flexcoders/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
RE: [flexcoders] Re: Singleton not usable?
That works in C# because the default access specifier for class members in C# is private. It won't work in ActionScript 3.0 because the constructor is always public, whether or not you declare it as such. Francis > -Original Message- > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On > Behalf Of crnet_flex > Sent: Friday, April 14, 2006 3:19 PM > To: flexcoders@yahoogroups.com > Subject: [flexcoders] Re: Singleton not usable? > > > Hi > > Have anyone tried using a construction like the following? > > public class Singleton > { > public static const Instance:Singleton = new Singleton(); > > function Singleton() > { > } > > public function doSomething():void > { > } > } > > This is actually the recommandations of creating a singleton in c#, > and it seems that it fullfils the job here as well. > > I know that, if you use flexbuilder 2 beta, then you'll get a > warning if you leave out the scope of the constructor, but maybe > it's worth it. > > By the way, be aware that the singleton posted initially as an > example is not a recommended way of creating a singleton, i guess > it's not a problem in AS3, but lazy loaded singletons must have a > syncronized/looked load section when thread safety is an issue. > > BR Casper -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/flexcoders/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
[flexcoders] Re: Singleton not usable?
--- In flexcoders@yahoogroups.com, "Sonja Duijvesteijn" <[EMAIL PROTECTED]> wrote: > > Hi all, > > I'm trying to make a singleton class as demonstrated below, but get the > error: "a constructor may only be declared 'public'". Well, that error is > clear, the reason for it is not however. > Isn't it possible anymore to make a singleton class, or is there another > method for it? > > Class Singleton { > > private static var _instance:Singleton = null; > > private function Sinleton() { > } > > public static function getInstance():Singleton { >if(Singleton._instance == null) { > _instance = new Singleton(); >} >return _instance; > } > > } > > Any help would be much appreciated. > > Kind regards, > Sonja Duijvesteijn > Hi Have anyone tried using a construction like the following? public class Singleton { public static const Instance:Singleton = new Singleton(); function Singleton() { } public function doSomething():void { } } This is actually the recommandations of creating a singleton in c#, and it seems that it fullfils the job here as well. I know that, if you use flexbuilder 2 beta, then you'll get a warning if you leave out the scope of the constructor, but maybe it's worth it. By the way, be aware that the singleton posted initially as an example is not a recommended way of creating a singleton, i guess it's not a problem in AS3, but lazy loaded singletons must have a syncronized/looked load section when thread safety is an issue. BR Casper -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/flexcoders/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/