Singletons

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Singletons

tom rhodes
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Michiel Crefcoeur
Change this line:
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );

Into this:
static inline var instance:LayoutManager = new LayoutManager( SingletonLock );

Since it is an inline var, it is by definition read-only and you can therefore not define any accessors for it.
You can remove the getInstance function.

2011/2/19 tom rhodes <[hidden email]>
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: Singletons

luca deltodesco
That does not create a singleton, using inline in that manner means that every time you access Assets.instance, you will get a new LayoutManager.


Date: Sat, 19 Feb 2011 20:06:24 +0100
Subject: Re: [haXe] Singletons
From: [hidden email]
To: [hidden email]
CC:

Change this line:
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );

Into this:
static inline var instance:LayoutManager = new LayoutManager( SingletonLock );

Since it is an inline var, it is by definition read-only and you can therefore not define any accessors for it.
You can remove the getInstance function.

2011/2/19 tom rhodes <[hidden email]>
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


-- haXe - an open source web programming language http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

tom rhodes
ouch. not what i'm looking for then!

what do i need to use instead to emulate the AS3 version?


On 19 February 2011 20:11, luca deltodesco <[hidden email]> wrote:
That does not create a singleton, using inline in that manner means that every time you access Assets.instance, you will get a new LayoutManager.


Date: Sat, 19 Feb 2011 20:06:24 +0100
Subject: Re: [haXe] Singletons
From: [hidden email]
To: [hidden email]
CC:

Change this line:
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );

Into this:
static inline var instance:LayoutManager = new LayoutManager( SingletonLock );

Since it is an inline var, it is by definition read-only and you can therefore not define any accessors for it.
You can remove the getInstance function.

2011/2/19 tom rhodes <[hidden email]>
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


-- haXe - an open source web programming language http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Michiel Crefcoeur
luca is totally right about that!
you can create a private static var which your getter can return.

2011/2/19 tom rhodes <[hidden email]>
ouch. not what i'm looking for then!

what do i need to use instead to emulate the AS3 version?



On 19 February 2011 20:11, luca deltodesco <[hidden email]> wrote:
That does not create a singleton, using inline in that manner means that every time you access Assets.instance, you will get a new LayoutManager.


Date: Sat, 19 Feb 2011 20:06:24 +0100
Subject: Re: [haXe] Singletons
From: [hidden email]
To: [hidden email]
CC:

Change this line:
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );

Into this:
static inline var instance:LayoutManager = new LayoutManager( SingletonLock );

Since it is an inline var, it is by definition read-only and you can therefore not define any accessors for it.
You can remove the getInstance function.

2011/2/19 tom rhodes <[hidden email]>
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


-- haXe - an open source web programming language http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Michiel Crefcoeur
Or better, use the default getter.

Change this line:
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );

Into this:
static var instance(default, never):LayoutManager = new LayoutManager( SingletonLock );

Sorry, my attention was a bit off. ;-)

2011/2/19 Michiel Crefcoeur <[hidden email]>
luca is totally right about that!
you can create a private static var which your getter can return.

2011/2/19 tom rhodes <[hidden email]>
ouch. not what i'm looking for then!

what do i need to use instead to emulate the AS3 version?



On 19 February 2011 20:11, luca deltodesco <[hidden email]> wrote:
That does not create a singleton, using inline in that manner means that every time you access Assets.instance, you will get a new LayoutManager.


Date: Sat, 19 Feb 2011 20:06:24 +0100
Subject: Re: [haXe] Singletons
From: [hidden email]
To: [hidden email]
CC:

Change this line:
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );

Into this:
static inline var instance:LayoutManager = new LayoutManager( SingletonLock );

Since it is an inline var, it is by definition read-only and you can therefore not define any accessors for it.
You can remove the getInstance function.

2011/2/19 tom rhodes <[hidden email]>
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


-- haXe - an open source web programming language http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Pimm Hogeling
In reply to this post by tom rhodes
Hi Tom,

You said what I need to do to get a nice singleton implementation. I would like to point out that the words nice and singleton should never be used in one sentence.

You could do something like this:
class Assets {
    /**
     * Determines whether an instance of this class can be created.
     */
    private static var canInstanciate:Bool;
    /**
     * The only instance of this class.
     */
    public static var instance(getInstance, null):Assets;
    public function new():Void {
        if (false == canInstanciate) {
            throw "Invalid Singleton access. Use Assets.instance.";
        }
    }
    private static function getInstance():Assets {
        if (null == instance) {
            // Set the flag to true, so the instance can be created.
            canInstanciate = true;
            // Create the only instance.
            instance = new Assets();
            // Set the flag back to false, so no further instances can be created.
            canInstanciate = false;
        }
        return instance;
    }
}

I recommend that any developer who wants something singletonised reconsiders at least trice. The problem with the singleton pattern is pretty straight forward: because the singleton pattern gives you a global access point to one instance, every line of code in your entire codebase can access that instance. Now, unless the class is immutable, every line of code in your codebase can also change the state of that instance, potentially breaking your entire application.

Furthermore, disabling the creation of multiple instances is almost never useful.

You might want to check out this talk by Misko Hevery.

My advice is, be extremely careful when using the singleton pattern, especially in projects with large codebases and multiple developers.

On 19 February 2011 19:57, tom rhodes <[hidden email]> wrote:
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}
what i need to do to get a nice singleton implementation
class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

tom rhodes
thanks for that, " I would like to point out that the words nice and singleton should never be used in one sentence." ahah. i have read some of the pros and cons of the approach, it's just that it really seems to make sense to me when i want say a pool of assets in an asset manager, all classes can dip in and get stuff when they need them etc.

mostly, apart from a loading method, the data/assets arre got at through read only properties after that so unless you reload then the other classes cannot change anything.

i'm always willing to learn though Pimm, and will check out the talk, just out of interest in the sort of case i've described briefly above, how would you make assets/data exist in one place and be available throughout the app where they might be needed?

always great info on this list, thanks,

tom.


On 19 February 2011 20:27, Pimm Hogeling <[hidden email]> wrote:
Hi Tom,

You said what I need to do to get a nice singleton implementation. I would like to point out that the words nice and singleton should never be used in one sentence.

You could do something like this:
class Assets {
    /**
     * Determines whether an instance of this class can be created.
     */
    private static var canInstanciate:Bool;
    /**
     * The only instance of this class.
     */
    public static var instance(getInstance, null):Assets;
    public function new():Void {
        if (false == canInstanciate) {
            throw "Invalid Singleton access. Use Assets.instance.";
        }
    }
    private static function getInstance():Assets {
        if (null == instance) {
            // Set the flag to true, so the instance can be created.
            canInstanciate = true;
            // Create the only instance.
            instance = new Assets();
            // Set the flag back to false, so no further instances can be created.
            canInstanciate = false;
        }
        return instance;
    }
}

I recommend that any developer who wants something singletonised reconsiders at least trice. The problem with the singleton pattern is pretty straight forward: because the singleton pattern gives you a global access point to one instance, every line of code in your entire codebase can access that instance. Now, unless the class is immutable, every line of code in your codebase can also change the state of that instance, potentially breaking your entire application.

Furthermore, disabling the creation of multiple instances is almost never useful.

You might want to check out this talk by Misko Hevery.

My advice is, be extremely careful when using the singleton pattern, especially in projects with large codebases and multiple developers.

On 19 February 2011 19:57, tom rhodes <[hidden email]> wrote:
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}
what i need to do to get a nice singleton implementation
class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

domrein
Just make your constructor private and you don't need a SingletonLock class.  The SingletonLock thing is a work around in AS3 to create singletons because you can't make constructors private, but that problem doesn't exist in haxe.  I'd agree about being really careful with singletons though.  They've come back to bite me quite a few times.

On Sat, Feb 19, 2011 at 1:29 PM, tom rhodes <[hidden email]> wrote:
thanks for that, " I would like to point out that the words nice and singleton should never be used in one sentence." ahah. i have read some of the pros and cons of the approach, it's just that it really seems to make sense to me when i want say a pool of assets in an asset manager, all classes can dip in and get stuff when they need them etc.

mostly, apart from a loading method, the data/assets arre got at through read only properties after that so unless you reload then the other classes cannot change anything.

i'm always willing to learn though Pimm, and will check out the talk, just out of interest in the sort of case i've described briefly above, how would you make assets/data exist in one place and be available throughout the app where they might be needed?

always great info on this list, thanks,

tom.



On 19 February 2011 20:27, Pimm Hogeling <[hidden email]> wrote:
Hi Tom,

You said what I need to do to get a nice singleton implementation. I would like to point out that the words nice and singleton should never be used in one sentence.

You could do something like this:
class Assets {
    /**
     * Determines whether an instance of this class can be created.
     */
    private static var canInstanciate:Bool;
    /**
     * The only instance of this class.
     */
    public static var instance(getInstance, null):Assets;
    public function new():Void {
        if (false == canInstanciate) {
            throw "Invalid Singleton access. Use Assets.instance.";
        }
    }
    private static function getInstance():Assets {
        if (null == instance) {
            // Set the flag to true, so the instance can be created.
            canInstanciate = true;
            // Create the only instance.
            instance = new Assets();
            // Set the flag back to false, so no further instances can be created.
            canInstanciate = false;
        }
        return instance;
    }
}

I recommend that any developer who wants something singletonised reconsiders at least trice. The problem with the singleton pattern is pretty straight forward: because the singleton pattern gives you a global access point to one instance, every line of code in your entire codebase can access that instance. Now, unless the class is immutable, every line of code in your codebase can also change the state of that instance, potentially breaking your entire application.

Furthermore, disabling the creation of multiple instances is almost never useful.

You might want to check out this talk by Misko Hevery.

My advice is, be extremely careful when using the singleton pattern, especially in projects with large codebases and multiple developers.

On 19 February 2011 19:57, tom rhodes <[hidden email]> wrote:
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}
what i need to do to get a nice singleton implementation
class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Dion Whitehead Amago
Regarding what you are trying to accomplish, in the PushButton game
engine (the haxe fork is Hydrax) the equivalent functionality is using
single managers that are kept in a game context specific map, where
"context" may be a game or a game level, for example.

This means that as far as game objects are concerned, there is only
instance on that class, accessed via a context field that all game
objects have injected.  This allows e.g. minigames, that might share
the parent games resource/asset manager, but no managers or singleton
classes are ever accessed statically.

In other words, the game, and subsets of the game, are separate
"contexts", and supply their child game objects with the relevant
singleton-like classes, so that no game objects interact with objects
outside of their "context", thus are safe from issues arising from
singletons.

On Sat, Feb 19, 2011 at 2:48 PM, Domrein Alabaster
<[hidden email]> wrote:

> Just make your constructor private and you don't need a SingletonLock class.
>  The SingletonLock thing is a work around in AS3 to create singletons
> because you can't make constructors private, but that problem doesn't exist
> in haxe.  I'd agree about being really careful with singletons though.
>  They've come back to bite me quite a few times.
>
> On Sat, Feb 19, 2011 at 1:29 PM, tom rhodes <[hidden email]> wrote:
>>
>> thanks for that, " I would like to point out that the
>> words nice and singleton should never be used in one sentence." ahah. i have
>> read some of the pros and cons of the approach, it's just that it really
>> seems to make sense to me when i want say a pool of assets in an asset
>> manager, all classes can dip in and get stuff when they need them etc.
>> mostly, apart from a loading method, the data/assets arre got at through
>> read only properties after that so unless you reload then the other classes
>> cannot change anything.
>> i'm always willing to learn though Pimm, and will check out the talk, just
>> out of interest in the sort of case i've described briefly above, how would
>> you make assets/data exist in one place and be available throughout the app
>> where they might be needed?
>> always great info on this list, thanks,
>> tom.
>>
>>
>> On 19 February 2011 20:27, Pimm Hogeling <[hidden email]> wrote:
>>>
>>> Hi Tom,
>>>
>>> You said what I need to do to get a nice singleton implementation. I
>>> would like to point out that the words nice and singleton should never be
>>> used in one sentence.
>>>
>>> You could do something like this:
>>> class Assets {
>>>     /**
>>>      * Determines whether an instance of this class can be created.
>>>      */
>>>     private static var canInstanciate:Bool;
>>>     /**
>>>      * The only instance of this class.
>>>      */
>>>     public static var instance(getInstance, null):Assets;
>>>     public function new():Void {
>>>         if (false == canInstanciate) {
>>>             throw "Invalid Singleton access. Use Assets.instance.";
>>>         }
>>>     }
>>>     private static function getInstance():Assets {
>>>         if (null == instance) {
>>>             // Set the flag to true, so the instance can be created.
>>>             canInstanciate = true;
>>>             // Create the only instance.
>>>             instance = new Assets();
>>>             // Set the flag back to false, so no further instances can be
>>> created.
>>>             canInstanciate = false;
>>>         }
>>>         return instance;
>>>     }
>>> }
>>>
>>> I recommend that any developer who wants something singletonised
>>> reconsiders at least trice. The problem with the singleton pattern is pretty
>>> straight forward: because the singleton pattern gives you a global access
>>> point to one instance, every line of code in your entire codebase can access
>>> that instance. Now, unless the class is immutable, every line of code in
>>> your codebase can also change the state of that instance, potentially
>>> breaking your entire application.
>>>
>>> Furthermore, disabling the creation of multiple instances is almost never
>>> useful.
>>>
>>> You might want to check out this talk by Misko Hevery.
>>>
>>> My advice is, be extremely careful when using the singleton pattern,
>>> especially in projects with large codebases and multiple developers.
>>>
>>> On 19 February 2011 19:57, tom rhodes <[hidden email]> wrote:
>>>>
>>>> Hi all,
>>>> i'm trying to find the haxe equivalent of my usual AS3 method of doing a
>>>> singleton...
>>>> public class Assets
>>>> {
>>>> private static const _instance:Assets = new Assets( SingletonLock );
>>>> public static function get instance():Assets
>>>> {
>>>> return _instance;
>>>> }
>>>> public function Assets(lock:Class)
>>>> {
>>>> if ( lock != SingletonLock )
>>>> {
>>>> throw new Error( "Invalid Singleton access.  Use Assets.instance." );
>>>> }
>>>> }
>>>> }
>>>> class SingletonLock
>>>> {}
>>>> but i'm having some trouble! i have read there is no const, so i'm
>>>> trying to use inline but i'm doing something wrong :(
>>>> class LayoutManager
>>>> {
>>>> static inline var instance(getInstance, never):LayoutManager = new
>>>> LayoutManager( SingletonLock );
>>>> public function new(lock:Class)
>>>> {
>>>> if ( lock != SingletonLock )
>>>> {
>>>> trace( "Invalid Singleton access.  Use LayoutManager.instance." );
>>>> }
>>>> }
>>>> private function getInstance():LayoutManager
>>>> {
>>>> return instance;
>>>> }
>>>> }
>>>> what i need to do to get a nice singleton implementation
>>>> class SingletonLock
>>>> {}
>>>> can anyone explain to me what's going on here and what i need to do to
>>>> get a nice singleton implementation?
>>>> thanks in advance...
>>>> tom.
>>>> --
>>>> haXe - an open source web programming language
>>>> http://haxe.org
>>>
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Re: [haXe] Singletons

Rob Fell
In reply to this post by tom rhodes
Hi Tom,

I'd echo Pimm's concerns - singletons will always create headaches
tomorrow.  But fortunately avoiding this today need not be a big
redesign :-)

My advice would be to look at your requirements the other way around.  
i.e. "Don't call us, we'll call you" / inversion of control:  inject
assets rather than have instances go fishing for them.

http://en.wikipedia.org/wiki/Hollywood_Principle

You retain just a single instance of the Asset class because it is
instantiated only once and passed around by interface (rather than
static address).  No singleton needed.

Best discipline is to inject only the exact requirements (usually in the
constructor), but it is sometimes pragmatic to inject a broader
AssetManager instance (project wide or object specific) - which may not
be disimilar to what you've already made in your Assets class (just not
a singleton)?

Hope that's helpful, Rob



On 11:59 AM, tom rhodes wrote:

> thanks for that, " I would like to point out that the words /nice/ and
> /singleton/ should never be used in one sentence." ahah. i have read
> some of the pros and cons of the approach, it's just that it really
> seems to make sense to me when i want say a pool of assets in an asset
> manager, all classes can dip in and get stuff when they need them etc.
>
> mostly, apart from a loading method, the data/assets arre got at
> through read only properties after that so unless you reload then the
> other classes cannot change anything.
>
> i'm always willing to learn though Pimm, and will check out the talk,
> just out of interest in the sort of case i've described briefly above,
> how would you make assets/data exist in one place and be available
> throughout the app where they might be needed?
>
> always great info on this list, thanks,
>
> tom.
>
>
> On 19 February 2011 20:27, Pimm Hogeling <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi Tom,
>
>     You said /what I need to do to get a nice singleton
>     implementation/. I would like to point out that the words /nice/
>     and /singleton/ should never be used in one sentence.
>
>     You could do something like this:
>     class Assets {
>         /**
>          * Determines whether an instance of this class can be created.
>          */
>         private static var canInstanciate:Bool;
>         /**
>          * The only instance of this class.
>          */
>         public static var instance(getInstance, null):Assets;
>         public function new():Void {
>             if (false == canInstanciate) {
>                 throw "Invalid Singleton access. Use Assets.instance.";
>             }
>         }
>         private static function getInstance():Assets {
>             if (null == instance) {
>                 // Set the flag to true, so the instance can be created.
>                 canInstanciate = true;
>                 // Create the only instance.
>                 instance = new Assets();
>                 // Set the flag back to false, so no further instances
>     can be created.
>                 canInstanciate = false;
>             }
>             return instance;
>         }
>     }
>
>     *I recommend that any developer who wants something singletonised
>     reconsiders at least trice*. The problem with the singleton
>     pattern is pretty straight forward: because the singleton pattern
>     gives you a global access point to one instance, every line of
>     code in your entire codebase can access that instance. Now, unless
>     the class is immutable, every line of code in your codebase can
>     also change the state of that instance, potentially breaking your
>     entire application.
>
>     Furthermore, disabling the creation of multiple instances is
>     almost never useful.
>
>     You might want to check out this talk by Misko Hevery
>     <http://youtu.be/-FRm3VPhseI>.
>
>     My advice is, be extremely careful when using the singleton
>     pattern, especially in projects with large codebases and multiple
>     developers.
>
>     On 19 February 2011 19:57, tom rhodes <[hidden email]
>     <mailto:[hidden email]>> wrote:
>
>         Hi all,
>
>         i'm trying to find the haxe equivalent of my usual AS3 method
>         of doing a singleton...
>
>         public class Assets
>         {
>         private static const _instance:Assets = new Assets(
>         SingletonLock );
>         public static function get instance():Assets
>         {
>         return _instance;
>         }
>         public function Assets(lock:Class)
>         {
>         if ( lock != SingletonLock )
>         {
>         throw new Error( "Invalid Singleton access.  Use
>         Assets.instance." );
>         }
>         }
>         }
>
>         class SingletonLock
>         {}
>
>         but i'm having some trouble! i have read there is no const, so
>         i'm trying to use inline but i'm doing something wrong :(
>
>         class LayoutManager
>         {
>         static inline var instance(getInstance, never):LayoutManager =
>         new LayoutManager( SingletonLock );
>         public function new(lock:Class)
>         {
>         if ( lock != SingletonLock )
>         {
>         trace( "Invalid Singleton access.  Use LayoutManager.instance." );
>         }
>         }
>         private function getInstance():LayoutManager
>         {
>         return instance;
>         }
>         }
>         what i need to do to get a nice singleton implementation
>         class SingletonLock
>         {}
>
>         can anyone explain to me what's going on here and what i need
>         to do to get a nice singleton implementation?
>
>         thanks in advance...
>
>         tom.
>
>         --
>         haXe - an open source web programming language
>         http://haxe.org
>
>
>
>     --
>     haXe - an open source web programming language
>     http://haxe.org
>
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Baluta Cristian
In reply to this post by Dion Whitehead Amago
I never saw a problem with singletons and i can't see how they are dangerous if you can't override them. For some functionalities i even have static methods that calls other methods in the instance, it's jus for saving a word when i call it.

On Sat, Feb 19, 2011 at 11:01 PM, Dion Amago <[hidden email]> wrote:
Regarding what you are trying to accomplish, in the PushButton game
engine (the haxe fork is Hydrax) the equivalent functionality is using
single managers that are kept in a game context specific map, where
"context" may be a game or a game level, for example.

This means that as far as game objects are concerned, there is only
instance on that class, accessed via a context field that all game
objects have injected.  This allows e.g. minigames, that might share
the parent games resource/asset manager, but no managers or singleton
classes are ever accessed statically.

In other words, the game, and subsets of the game, are separate
"contexts", and supply their child game objects with the relevant
singleton-like classes, so that no game objects interact with objects
outside of their "context", thus are safe from issues arising from
singletons.

On Sat, Feb 19, 2011 at 2:48 PM, Domrein Alabaster
<[hidden email]> wrote:
> Just make your constructor private and you don't need a SingletonLock class.
>  The SingletonLock thing is a work around in AS3 to create singletons
> because you can't make constructors private, but that problem doesn't exist
> in haxe.  I'd agree about being really careful with singletons though.
>  They've come back to bite me quite a few times.
>
> On Sat, Feb 19, 2011 at 1:29 PM, tom rhodes <[hidden email]> wrote:
>>
>> thanks for that, " I would like to point out that the
>> words nice and singleton should never be used in one sentence." ahah. i have
>> read some of the pros and cons of the approach, it's just that it really
>> seems to make sense to me when i want say a pool of assets in an asset
>> manager, all classes can dip in and get stuff when they need them etc.
>> mostly, apart from a loading method, the data/assets arre got at through
>> read only properties after that so unless you reload then the other classes
>> cannot change anything.
>> i'm always willing to learn though Pimm, and will check out the talk, just
>> out of interest in the sort of case i've described briefly above, how would
>> you make assets/data exist in one place and be available throughout the app
>> where they might be needed?
>> always great info on this list, thanks,
>> tom.
>>
>>
>> On 19 February 2011 20:27, Pimm Hogeling <[hidden email]> wrote:
>>>
>>> Hi Tom,
>>>
>>> You said what I need to do to get a nice singleton implementation. I
>>> would like to point out that the words nice and singleton should never be
>>> used in one sentence.
>>>
>>> You could do something like this:
>>> class Assets {
>>>     /**
>>>      * Determines whether an instance of this class can be created.
>>>      */
>>>     private static var canInstanciate:Bool;
>>>     /**
>>>      * The only instance of this class.
>>>      */
>>>     public static var instance(getInstance, null):Assets;
>>>     public function new():Void {
>>>         if (false == canInstanciate) {
>>>             throw "Invalid Singleton access. Use Assets.instance.";
>>>         }
>>>     }
>>>     private static function getInstance():Assets {
>>>         if (null == instance) {
>>>             // Set the flag to true, so the instance can be created.
>>>             canInstanciate = true;
>>>             // Create the only instance.
>>>             instance = new Assets();
>>>             // Set the flag back to false, so no further instances can be
>>> created.
>>>             canInstanciate = false;
>>>         }
>>>         return instance;
>>>     }
>>> }
>>>
>>> I recommend that any developer who wants something singletonised
>>> reconsiders at least trice. The problem with the singleton pattern is pretty
>>> straight forward: because the singleton pattern gives you a global access
>>> point to one instance, every line of code in your entire codebase can access
>>> that instance. Now, unless the class is immutable, every line of code in
>>> your codebase can also change the state of that instance, potentially
>>> breaking your entire application.
>>>
>>> Furthermore, disabling the creation of multiple instances is almost never
>>> useful.
>>>
>>> You might want to check out this talk by Misko Hevery.
>>>
>>> My advice is, be extremely careful when using the singleton pattern,
>>> especially in projects with large codebases and multiple developers.
>>>
>>> On 19 February 2011 19:57, tom rhodes <[hidden email]> wrote:
>>>>
>>>> Hi all,
>>>> i'm trying to find the haxe equivalent of my usual AS3 method of doing a
>>>> singleton...
>>>> public class Assets
>>>> {
>>>> private static const _instance:Assets = new Assets( SingletonLock );
>>>> public static function get instance():Assets
>>>> {
>>>> return _instance;
>>>> }
>>>> public function Assets(lock:Class)
>>>> {
>>>> if ( lock != SingletonLock )
>>>> {
>>>> throw new Error( "Invalid Singleton access.  Use Assets.instance." );
>>>> }
>>>> }
>>>> }
>>>> class SingletonLock
>>>> {}
>>>> but i'm having some trouble! i have read there is no const, so i'm
>>>> trying to use inline but i'm doing something wrong :(
>>>> class LayoutManager
>>>> {
>>>> static inline var instance(getInstance, never):LayoutManager = new
>>>> LayoutManager( SingletonLock );
>>>> public function new(lock:Class)
>>>> {
>>>> if ( lock != SingletonLock )
>>>> {
>>>> trace( "Invalid Singleton access.  Use LayoutManager.instance." );
>>>> }
>>>> }
>>>> private function getInstance():LayoutManager
>>>> {
>>>> return instance;
>>>> }
>>>> }
>>>> what i need to do to get a nice singleton implementation
>>>> class SingletonLock
>>>> {}
>>>> can anyone explain to me what's going on here and what i need to do to
>>>> get a nice singleton implementation?
>>>> thanks in advance...
>>>> tom.
>>>> --
>>>> haXe - an open source web programming language
>>>> http://haxe.org
>>>
>>>
>>> --
>>> haXe - an open source web programming language
>>> http://haxe.org
>>
>>
>> --
>> haXe - an open source web programming language
>> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org



--
Băluță Cristian
http://ralcr.com
http://imagin.ro

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Re: [haXe] Singletons

tom rhodes
In reply to this post by Rob Fell
Cheers Rob,

yup i'm starting out on a few haxe experiments and projects which will be a bit more akin to the sort of commercial work i've been doing with flash for ages, just hoping to hit broader targets, hence haxe. so that said, no redesigning :) and i'm grateful for the info i'm getting, some is very eye opening. the dependency injection stuff is interesting so i'll have a play with that as an idea i think. although it does seem to be how i used to approach things, passing references around to everything that might need it... although i probably just don't get it yet as you mentioned "by interface" and not by reference...

tom.


On 19 February 2011 23:01, Rob Fell <[hidden email]> wrote:
Hi Tom,

I'd echo Pimm's concerns - singletons will always create headaches tomorrow.  But fortunately avoiding this today need not be a big redesign :-)

My advice would be to look at your requirements the other way around.  i.e. "Don't call us, we'll call you" / inversion of control:  inject assets rather than have instances go fishing for them.

http://en.wikipedia.org/wiki/Hollywood_Principle

You retain just a single instance of the Asset class because it is instantiated only once and passed around by interface (rather than static address).  No singleton needed.

Best discipline is to inject only the exact requirements (usually in the constructor), but it is sometimes pragmatic to inject a broader AssetManager instance (project wide or object specific) - which may not be disimilar to what you've already made in your Assets class (just not a singleton)?

Hope that's helpful, Rob




On 11:59 AM, tom rhodes wrote:
thanks for that, " I would like to point out that the words /nice/ and /singleton/ should never be used in one sentence." ahah. i have read some of the pros and cons of the approach, it's just that it really seems to make sense to me when i want say a pool of assets in an asset manager, all classes can dip in and get stuff when they need them etc.

mostly, apart from a loading method, the data/assets arre got at through read only properties after that so unless you reload then the other classes cannot change anything.

i'm always willing to learn though Pimm, and will check out the talk, just out of interest in the sort of case i've described briefly above, how would you make assets/data exist in one place and be available throughout the app where they might be needed?

always great info on this list, thanks,

tom.


On 19 February 2011 20:27, Pimm Hogeling <[hidden email] <mailto:[hidden email]>> wrote:

   Hi Tom,

   You said /what I need to do to get a nice singleton
   implementation/. I would like to point out that the words /nice/
   and /singleton/ should never be used in one sentence.

   You could do something like this:
   class Assets {
       /**
        * Determines whether an instance of this class can be created.
        */
       private static var canInstanciate:Bool;
       /**
        * The only instance of this class.
        */
       public static var instance(getInstance, null):Assets;
       public function new():Void {
           if (false == canInstanciate) {
               throw "Invalid Singleton access. Use Assets.instance.";
           }
       }
       private static function getInstance():Assets {
           if (null == instance) {
               // Set the flag to true, so the instance can be created.
               canInstanciate = true;
               // Create the only instance.
               instance = new Assets();
               // Set the flag back to false, so no further instances
   can be created.
               canInstanciate = false;
           }
           return instance;
       }
   }

   *I recommend that any developer who wants something singletonised
   reconsiders at least trice*. The problem with the singleton
   pattern is pretty straight forward: because the singleton pattern
   gives you a global access point to one instance, every line of
   code in your entire codebase can access that instance. Now, unless
   the class is immutable, every line of code in your codebase can
   also change the state of that instance, potentially breaking your
   entire application.

   Furthermore, disabling the creation of multiple instances is
   almost never useful.

   You might want to check out this talk by Misko Hevery
   <http://youtu.be/-FRm3VPhseI>.


   My advice is, be extremely careful when using the singleton
   pattern, especially in projects with large codebases and multiple
   developers.

   On 19 February 2011 19:57, tom rhodes <[hidden email]
   <mailto:[hidden email]>> wrote:

       Hi all,

       i'm trying to find the haxe equivalent of my usual AS3 method
       of doing a singleton...

       public class Assets
       {
       private static const _instance:Assets = new Assets(
       SingletonLock );
       public static function get instance():Assets
       {
       return _instance;
       }
       public function Assets(lock:Class)
       {
       if ( lock != SingletonLock )
       {
       throw new Error( "Invalid Singleton access.  Use
       Assets.instance." );
       }
       }
       }

       class SingletonLock
       {}

       but i'm having some trouble! i have read there is no const, so
       i'm trying to use inline but i'm doing something wrong :(

       class LayoutManager
       {
       static inline var instance(getInstance, never):LayoutManager =
       new LayoutManager( SingletonLock );
       public function new(lock:Class)
       {
       if ( lock != SingletonLock )
       {
       trace( "Invalid Singleton access.  Use LayoutManager.instance." );
       }
       }
       private function getInstance():LayoutManager
       {
       return instance;
       }
       }
       what i need to do to get a nice singleton implementation
       class SingletonLock
       {}

       can anyone explain to me what's going on here and what i need
       to do to get a nice singleton implementation?

       thanks in advance...

       tom.

       --
       haXe - an open source web programming language
       http://haxe.org



   --
   haXe - an open source web programming language
   http://haxe.org



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

Pimm Hogeling
In reply to this post by tom rhodes
First of all, as Domrein pointed out, haXe has private constructors. I forgot, as I never write themmyself. Because of this, you can have this cleaner solution:
class Assets {
    /**
     * The only instance of this class.
     */
    public static var instance(default, null):Assets;
    private function new():Void {
    }
    private static function getInstance():Assets {
        if (null == instance) {
            // Create the only instance.
            instance = new Assets();
        }
        return instance;
    }
}

Tom, in case you are loading assets to use throughout your application, the singleton pattern might not be a bad choice. It really depends on the situation.

The singleton pattern is probably faster to implement, and might give you slightly better performance.

On the other hand, with dependency injection slash the Hollywood-principle you will probably end up with better readable code (as it is more obvious which class requires/uses which asset). Readability is especially important if multiple developers work on the project, or parts of the project are to be re-used. In addition, a singleton-less design is probably more flexible: changing a skin or something like that might involve replacing the assets for only part of the application, which is one of those things the singleton pattern can make ridiculously difficult.

Bottom line: it depends on the situation. Just keep in mind: the singleton pattern is not always a good idea.

On 19 February 2011 21:29, tom rhodes <[hidden email]> wrote:
thanks for that, " I would like to point out that the words nice and singleton should never be used in one sentence." ahah. i have read some of the pros and cons of the approach, it's just that it really seems to make sense to me when i want say a pool of assets in an asset manager, all classes can dip in and get stuff when they need them etc.

mostly, apart from a loading method, the data/assets arre got at through read only properties after that so unless you reload then the other classes cannot change anything.

i'm always willing to learn though Pimm, and will check out the talk, just out of interest in the sort of case i've described briefly above, how would you make assets/data exist in one place and be available throughout the app where they might be needed?

always great info on this list, thanks,

tom.



On 19 February 2011 20:27, Pimm Hogeling <[hidden email]> wrote:
Hi Tom,

You said what I need to do to get a nice singleton implementation. I would like to point out that the words nice and singleton should never be used in one sentence.

You could do something like this:
class Assets {
    /**
     * Determines whether an instance of this class can be created.
     */
    private static var canInstanciate:Bool;
    /**
     * The only instance of this class.
     */
    public static var instance(getInstance, null):Assets;
    public function new():Void {
        if (false == canInstanciate) {
            throw "Invalid Singleton access. Use Assets.instance.";
        }
    }
    private static function getInstance():Assets {
        if (null == instance) {
            // Set the flag to true, so the instance can be created.
            canInstanciate = true;
            // Create the only instance.
            instance = new Assets();
            // Set the flag back to false, so no further instances can be created.
            canInstanciate = false;
        }
        return instance;
    }
}

I recommend that any developer who wants something singletonised reconsiders at least trice. The problem with the singleton pattern is pretty straight forward: because the singleton pattern gives you a global access point to one instance, every line of code in your entire codebase can access that instance. Now, unless the class is immutable, every line of code in your codebase can also change the state of that instance, potentially breaking your entire application.

Furthermore, disabling the creation of multiple instances is almost never useful.

You might want to check out this talk by Misko Hevery.

My advice is, be extremely careful when using the singleton pattern, especially in projects with large codebases and multiple developers.

On 19 February 2011 19:57, tom rhodes <[hidden email]> wrote:
Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}
what i need to do to get a nice singleton implementation
class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Singletons

John A. De Goes
In reply to this post by tom rhodes

Use Stax dependency injector:

var assets = Assets.inject();

This way you can use different "singletons" for testing than for production.

Regards,

John A. De Goes
Twitter: @jdegoes 
LinkedIn: http://linkedin.com/in/jdegoes

On Feb 19, 2011, at 1:57 PM, tom rhodes wrote:

Hi all,

i'm trying to find the haxe equivalent of my usual AS3 method of doing a singleton...

public class Assets
{
private static const _instance:Assets = new Assets( SingletonLock );
public static function get instance():Assets
{
return _instance;
}
public function Assets(lock:Class) 
{
if ( lock != SingletonLock )
{
throw new Error( "Invalid Singleton access.  Use Assets.instance." );
}
}
}

class SingletonLock
{}

but i'm having some trouble! i have read there is no const, so i'm trying to use inline but i'm doing something wrong :(

class LayoutManager 
{
static inline var instance(getInstance, never):LayoutManager = new LayoutManager( SingletonLock );
public function new(lock:Class) 
{
if ( lock != SingletonLock )
{
trace( "Invalid Singleton access.  Use LayoutManager.instance." );
}
}
private function getInstance():LayoutManager
{
return instance;
}
}

class SingletonLock
{}

can anyone explain to me what's going on here and what i need to do to get a nice singleton implementation?

thanks in advance...

tom.
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org