RE: [Flashcoders] Singleton in AS3

2005-11-14 Thread zwetan

 Have anyone got ideas how to implement Singleton pattern in AS3 best?
 Constructor in AS3 can only be public or internal, which is not private
 anyway. ..


Well, internal should be enough to implement a Singleton


Just have a public const in the package initialized with an internal class
of the package


Something like that

myTest/MySingleton.as
-
package myTest
  {
  public const MySingleton:Singleton = new Singleton();
  }
-

myTest/Singleton.as
-
Package myTest
  {

  //default to internal, can only be accessed inside the package
  class Singleton
 {

 //be sure to let the constructor internal
 function Singleton()
{
//...
}

 //be sure to have your methods/properties/etc. public
 //so it can be accessed outside of the package
 public function someMethod():String
{
//...
}
 }
  }
-

And after well

import myTest.MySingleton;

MySingleton.someMethod()
etc.


I don't see the need of a private constructors at all

And furthermore private constructors imho would cause
more problems than solutions

What people oversee is that we are in ECMAScript,
A Singleton in this context should just be an object
That you can not copy and/or inherit in another prototype
So a public read-only object.

Here having an internal class inside a package prevent
to instanciate this class outside of the package
the public const force to have only one global access point to the
instancied object
So where is the problem ?

Not having private constructors is not a bug,
It's using a private constructor to implement singleton
which is imho a bug/hack from AS2 times.

http://www.mozilla.org/js/language/es4/core/definitions.html

private - Makes the definition visible only in the enclosing class's private
namespace

so following the standard if you got a private constructor
you just CAN NOT instanciate at all the class anywhere else
than inside the class !

private constructors just does not make sens.

zwetan



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Singleton in AS3

2005-11-14 Thread zwetan

 They do. With internal access modifier you're still able to create an
 instance of class directly.


Not outside of the package, or if you have a code exemple
That show that behaviour I would be interested to see it.

zwetan




___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Singleton in AS3

2005-11-11 Thread JesterXL
You can't yet as for the reason you stated.  Bitch on the forums pretty 
please; you, many many others, and I all want this changed.

http://www.macromedia.com/cfusion/webforums/forum/categories.cfm?catid=587entercat=y

- Original Message - 
From: Michael Klishin [EMAIL PROTECTED]
To: Flashcoders ML flashcoders@chattyfig.figleaf.com
Sent: Thursday, November 10, 2005 12:16 PM
Subject: [Flashcoders] Singleton in AS3


Hi guys,

Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..

-- 
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
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


RE: [Flashcoders] Singleton in AS3

2005-11-11 Thread Chris Wilson
Why should a public constructor prevent the creation of a Singleton?  All
you need is a static getInstance() method and a private attribute to hold
the created instance.  Sure, the public constructor means someone else could
instantiate the class on their own without using getInstance(), but if
you're writing the code, you'll do it the proper way anyhow, no?

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:32 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

You can't yet as for the reason you stated.  Bitch on the forums pretty 
please; you, many many others, and I all want this changed.

http://www.macromedia.com/cfusion/webforums/forum/categories.cfm?catid=587e
ntercat=y

- Original Message - 
From: Michael Klishin [EMAIL PROTECTED]
To: Flashcoders ML flashcoders@chattyfig.figleaf.com
Sent: Thursday, November 10, 2005 12:16 PM
Subject: [Flashcoders] Singleton in AS3


Hi guys,

Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..

-- 
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
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


Re: [Flashcoders] Singleton in AS3

2005-11-11 Thread JesterXL
That's fine for use Flashers who are used to using such methods to get what 
we want.

However, other languages are more strict, and from the perception of new 
programmers coming into ActionScript 3, not meeting their expectations is a 
failure of the language... to point.  Bottom line, they see our methods as 
hacks, and that's a bad thing.

The correct way is to use getInstance, yes.  But, it should also throw an 
exception if you try to instantiate it:

var a:MyClass = new MyClass();

That should not work, but it does currently because you have no choice.

In an effort to make ActionScript more clean, strict, and helpful, this is 
one of the additions.  The language itself should be self-explanatory to use 
it, and should support common programming patterns such as Singleton. 
Supporting design patterns in a language, to me is iffy, but there are a 
enough business use cases in my opinion to support it.

- Original Message - 
From: Chris Wilson [EMAIL PROTECTED]
To: 'Flashcoders mailing list' flashcoders@chattyfig.figleaf.com
Sent: Friday, November 11, 2005 1:40 PM
Subject: RE: [Flashcoders] Singleton in AS3


Why should a public constructor prevent the creation of a Singleton?  All
you need is a static getInstance() method and a private attribute to hold
the created instance.  Sure, the public constructor means someone else could
instantiate the class on their own without using getInstance(), but if
you're writing the code, you'll do it the proper way anyhow, no?

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:32 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

You can't yet as for the reason you stated.  Bitch on the forums pretty
please; you, many many others, and I all want this changed.

http://www.macromedia.com/cfusion/webforums/forum/categories.cfm?catid=587e
ntercat=y

- Original Message - 
From: Michael Klishin [EMAIL PROTECTED]
To: Flashcoders ML flashcoders@chattyfig.figleaf.com
Sent: Thursday, November 10, 2005 12:16 PM
Subject: [Flashcoders] Singleton in AS3


Hi guys,

Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..

-- 
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
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 

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Singleton in AS3

2005-11-11 Thread Chris Wilson
Agreed on all points, but when the deadline is approaching and the code
needs to be done...

Ironically to your points, I'm relatively new to Flash (~2 months), and have
come to this language from C, C++, and Java.  It's been *far* more difficult
to learn than I expected, in many cases due to the hacks you mentioned.
(Whaddya' mean 'this' doesn't refer to the class instance when you're in a
callback function _that's_a_method_of_the_class_!?!)  

That said, I'm a pragmatist when it comes to finishing a project.  :-]

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:51 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

That's fine for use Flashers who are used to using such methods to get what 
we want.

However, other languages are more strict, and from the perception of new 
programmers coming into ActionScript 3, not meeting their expectations is a 
failure of the language... to point.  Bottom line, they see our methods as

hacks, and that's a bad thing.

The correct way is to use getInstance, yes.  But, it should also throw an 
exception if you try to instantiate it:

var a:MyClass = new MyClass();

That should not work, but it does currently because you have no choice.

In an effort to make ActionScript more clean, strict, and helpful, this is 
one of the additions.  The language itself should be self-explanatory to use

it, and should support common programming patterns such as Singleton. 
Supporting design patterns in a language, to me is iffy, but there are a 
enough business use cases in my opinion to support it.

- Original Message - 
From: Chris Wilson [EMAIL PROTECTED]
To: 'Flashcoders mailing list' flashcoders@chattyfig.figleaf.com
Sent: Friday, November 11, 2005 1:40 PM
Subject: RE: [Flashcoders] Singleton in AS3


Why should a public constructor prevent the creation of a Singleton?  All
you need is a static getInstance() method and a private attribute to hold
the created instance.  Sure, the public constructor means someone else could
instantiate the class on their own without using getInstance(), but if
you're writing the code, you'll do it the proper way anyhow, no?

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:32 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

You can't yet as for the reason you stated.  Bitch on the forums pretty
please; you, many many others, and I all want this changed.

http://www.macromedia.com/cfusion/webforums/forum/categories.cfm?catid=587e
ntercat=y

- Original Message - 
From: Michael Klishin [EMAIL PROTECTED]
To: Flashcoders ML flashcoders@chattyfig.figleaf.com
Sent: Thursday, November 10, 2005 12:16 PM
Subject: [Flashcoders] Singleton in AS3


Hi guys,

Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..

-- 
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
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 

___
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


Re: [Flashcoders] Singleton in AS3

2005-11-11 Thread JesterXL
I totally agree bro... but ActionScript is moving away from a pure RAD type 
of language, hence being more strict.

Exactly; hopefully people from your background won't have pain in the 
future.

Again, as it's been funnily pointed out, Ruby and Java both have been trying 
to get more dynamic why we get more strict, but bottom line, both started 
from different points and have different business goals.

- Original Message - 
From: Chris Wilson [EMAIL PROTECTED]
To: 'Flashcoders mailing list' flashcoders@chattyfig.figleaf.com
Sent: Friday, November 11, 2005 1:59 PM
Subject: RE: [Flashcoders] Singleton in AS3


Agreed on all points, but when the deadline is approaching and the code
needs to be done...

Ironically to your points, I'm relatively new to Flash (~2 months), and have
come to this language from C, C++, and Java.  It's been *far* more difficult
to learn than I expected, in many cases due to the hacks you mentioned.
(Whaddya' mean 'this' doesn't refer to the class instance when you're in a
callback function _that's_a_method_of_the_class_!?!)

That said, I'm a pragmatist when it comes to finishing a project.  :-]

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:51 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

That's fine for use Flashers who are used to using such methods to get what
we want.

However, other languages are more strict, and from the perception of new
programmers coming into ActionScript 3, not meeting their expectations is a
failure of the language... to point.  Bottom line, they see our methods as

hacks, and that's a bad thing.

The correct way is to use getInstance, yes.  But, it should also throw an
exception if you try to instantiate it:

var a:MyClass = new MyClass();

That should not work, but it does currently because you have no choice.

In an effort to make ActionScript more clean, strict, and helpful, this is
one of the additions.  The language itself should be self-explanatory to use

it, and should support common programming patterns such as Singleton.
Supporting design patterns in a language, to me is iffy, but there are a
enough business use cases in my opinion to support it.

- Original Message - 
From: Chris Wilson [EMAIL PROTECTED]
To: 'Flashcoders mailing list' flashcoders@chattyfig.figleaf.com
Sent: Friday, November 11, 2005 1:40 PM
Subject: RE: [Flashcoders] Singleton in AS3


Why should a public constructor prevent the creation of a Singleton?  All
you need is a static getInstance() method and a private attribute to hold
the created instance.  Sure, the public constructor means someone else could
instantiate the class on their own without using getInstance(), but if
you're writing the code, you'll do it the proper way anyhow, no?

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:32 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

You can't yet as for the reason you stated.  Bitch on the forums pretty
please; you, many many others, and I all want this changed.

http://www.macromedia.com/cfusion/webforums/forum/categories.cfm?catid=587e
ntercat=y

- Original Message - 
From: Michael Klishin [EMAIL PROTECTED]
To: Flashcoders ML flashcoders@chattyfig.figleaf.com
Sent: Thursday, November 10, 2005 12:16 PM
Subject: [Flashcoders] Singleton in AS3


Hi guys,

Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..

-- 
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
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

___
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


Re: [Flashcoders] Singleton in AS3

2005-11-11 Thread Michael Klishin

Chris Wilson wrote:

Agreed on all points, but when the deadline is approaching and the code
needs to be done...


That said, I'm a pragmatist when it comes to finishing a project.  :-]



For sure. But as long as AS3 is going to become one of senior 
programming languages... ;)


--
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Singleton in AS3

2005-11-11 Thread Luke Bayes
 Have anyone got ideas how to implement Singleton pattern in AS3 best?
 Constructor in AS3 can only be public or internal, which is not private
 anyway


Hey Michael,

One way (as mentioned in another email) is to throw an error in the
constructor if some restriction has not been met. The following example may
not be the *greatest* hack, because there really isn't any way to catch the
error that's being thrown. But it's a hack that gets you to a singleton with
pretty high confidence that it was created via the factory method. Another
(more complicated) alternative is to use some convoluted namespace scheme.
Another (less reliable) possibility is to simply pass an argument to the
constructor - and throw if the argument is null.

I have to admit that I've been one of the most vocal proponents for the
private constructor argument, but want to make the point that the Singleton
pattern should be avoided whenever possible. It's very difficult to isolate
and test singletons, you wind up needing to add a destroy method or some
such other hack.

In my experience, almost every time I find myself leaning toward a
Singleton, I find a more elegant OO solution.

Here's a pretty good discussion about why to avoid Singletons:
http://www.softwarereality.com/design/singleton.jsp


I hope this helps.

Luke Bayes
www.asunit.org http://www.asunit.org


package {
import flash.util.trace;
import flash.errors.IllegalOperationError;
import flash.util.setTimeout;

public class SingletonExample {
private static var instance:SingletonExample;

public function SingletonExample(isSingleton:Boolean = false) {
setTimeout(checkSingleton, 1);
}

private function checkSingleton():Void {
if(this != SingletonExample.instance) {
throw new IllegalOperationError(SingletonExample instantiated directly, you
should use the static getInstance method instead);
}
}

public static function getInstance():SingletonExample {
if(SingletonExample.instance == null) {
SingletonExample.instance = new SingletonExample(true);
}

return SingletonExample.instance;
}

public function toString():String {
return Singleton instance created successfully;
}
}
}
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Singleton in AS3

2005-11-11 Thread JesterXL
Security by obscurity, while applicable  satisfying pioneers  early 
adopters, is still unacceptable; we need private constructors.

However, you do bring up a good point; I think, non intentionally, the new 
mx framework works like that; like, they use namespaces, in part to support 
namespaces for MXML (I think).

- Original Message - 
From: Fruber Malcome [EMAIL PROTECTED]
To: 'Flashcoders mailing list' flashcoders@chattyfig.figleaf.com
Sent: Friday, November 11, 2005 10:24 PM
Subject: RE: [Flashcoders] Singleton in AS3


There are many cases where you'd want to ensure that someone can't create a
new instance of a class that you've published.

An interesting workaround is to hide your class in a namespace that is 'not
publicly known', then write a wrapper class that someone can call NEW on -
and the constructor of that class checks to see if that object exists.

This doesn't ensure that it doesn't happen, but someone would have to have
intimate knowledge of some unknown namespace in order to create an instance
of that object..

Just an idea.

Thanks - Fruber


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Chris Wilson
Sent: Friday, November 11, 2005 10:40 AM
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Singleton in AS3

Why should a public constructor prevent the creation of a Singleton?  All
you need is a static getInstance() method and a private attribute to hold
the created instance.  Sure, the public constructor means someone else could
instantiate the class on their own without using getInstance(), but if
you're writing the code, you'll do it the proper way anyhow, no?

-Chris

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of JesterXL
Sent: Friday, November 11, 2005 1:32 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton in AS3

You can't yet as for the reason you stated.  Bitch on the forums pretty
please; you, many many others, and I all want this changed.

http://www.macromedia.com/cfusion/webforums/forum/categories.cfm?catid=587e
ntercat=y

- Original Message -
From: Michael Klishin [EMAIL PROTECTED]
To: Flashcoders ML flashcoders@chattyfig.figleaf.com
Sent: Thursday, November 10, 2005 12:16 PM
Subject: [Flashcoders] Singleton in AS3


Hi guys,

Have anyone got ideas how to implement Singleton pattern in AS3 best?
Constructor in AS3 can only be public or internal, which is not private
anyway. ..

-- 
Michael Antares Klishin,

Email: [EMAIL PROTECTED]
Web: www.novemberain.com

Non progredi est regredi
___
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
___
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