January 16, 2004
"davepermen" <davepermen_member@pathlink.com> wrote in message news:bu1coo$d8u$1@digitaldaemon.com...
> i know that they don't theoretically have much in common. but in real
life, all
> newbies abuse singletons to fit a nice oo-cover around their globals.

There is still some benefit to laying out your memory usage at compile time. Depending on your design, some parts of the system may in fact be best modeled as globals.  For a game, the video device, file system, system timer, mouse, keyboard, sound card, etc are all going to stay the same for the duration of the program, so why bother instantiating them on the heap?

> In article <bu1bk1$b7n$1@digitaldaemon.com>, Matthias Becker says...
> >
> >>as singletons are nothing more than that (globals), yes.. the real
module
> >>support of D makes it possible to have them more easy than in c++
> >A Singleton is a class that can be instantiated only once. Nothing else.
A
> >global is a variable that is accessable everywhere. I don't see what
these two
> >definitions have in common.
> >You could make the instance of the singleton be a global, but you don't
have to.

You are right.  They are really two separate issues.  In practice most of my "make once" singletons also need a single point of access.

A namespace in C++ is probably the closest thing to a singleton that it has. But then you still have inter-module startup/shutdown order problems.

Any other use for a singleton doesn't seem very useful... why make a class if you're only going to use it once?  To get automatic cleanup?  Do you write your function bodies as classes?

Sean


January 16, 2004
What thread nasties ? if you want to make a template synchronized class, go ahead. Nothing stops you.

As for the dead reference problems, since the internal pointer is never nullified, the object will not go away, unless the singleton class is destroyed. But singletons are supposed to be used that way.

I don't see any of the problems you are mentioning.

"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:bu27c5$1q90$1@digitaldaemon.com...
> No. That leaves you with all the thread nasties and ordering and dead reference problems.
>
> A valid compromise is to have the singleton notionally initialised during module initialisation - a wonderful boon from D, and not to be wasted -
and
> then to have any expensive state created lazily, in a thread-safe fashion. The state is then reference-counted by the module un/initialisation state, so there are no dead-references.
>
>
>
>
>
>
> "Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu21hi$1h9a$1@digitaldaemon.com...
> > The best solution is to define a template which creates one instance,
when
> > this instance is requested. For example:
> >
> > template <class T> class Singleton {
> >     private T *_object;
> >     T *object() {
> >         if (_object == null) _object = new T();
> >         return _object;
> >     }
> > }
> >
> >
> > "Phill" <phill@pacific.net.au> wrote in message news:bu20mp$1ft2$1@digitaldaemon.com...
> > > You should be able to implement a Singleton pattern
> > > easy.
> > > This is what I do in Java
> > >
> > > public class One{
> > >      public static One one;
> > >
> > > public static void getOne(){
> > >         if(one == null){
> > >               one new One();
> > >             }
> > >     }
> > >
> > > private One(){ // private constructor
> > >    //construct stuff
> > > }
> > >
> > > }
> > >
> > > This way if there is an instance of One already
> > > you cannot make another One.
> > >
> > > This was good for me in Java because I found
> > > that when clicking a JTable for some reason the
> > > Thread goes through the event listener method
> > > twice.
> > >
> > > The Singleton pattern solved this problem for
> > > me.
> > >
> > >
> > > Phill.
> > >
> > >
> > >
> > >
> > > "imr1984" <imr1984_member@pathlink.com> wrote in message news:bu0da8$1qic$1@digitaldaemon.com...
> > > > Often programmers make classes that are only intended to have one
> > instance
> > > for
> > > > the duration of the whole program. These are called singleton
classes.
> D
> > > should
> > > > have built in support for them so that a singleton object will have
> its
> > > own
> > > > methods generated for it, without the use of a this pointer. This
> would
> > > save on
> > > > a this parameter being pushed on stack to a singleton class method.
> > > >
> > > > Walter, what da ya say?
> > > >
> > > >
> > >
> > >
> >
> >
>
>
>


January 17, 2004
The threading problem's very simple: two or more threads may call object() for "the first time" at the same time.

Q: how many instances of T get created?



"Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu9lqn$20fo$1@digitaldaemon.com...
> What thread nasties ? if you want to make a template synchronized class,
go
> ahead. Nothing stops you.
>
> As for the dead reference problems, since the internal pointer is never nullified, the object will not go away, unless the singleton class is destroyed. But singletons are supposed to be used that way.
>
> I don't see any of the problems you are mentioning.
>
> "Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:bu27c5$1q90$1@digitaldaemon.com...
> > No. That leaves you with all the thread nasties and ordering and dead reference problems.
> >
> > A valid compromise is to have the singleton notionally initialised
during
> > module initialisation - a wonderful boon from D, and not to be wasted -
> and
> > then to have any expensive state created lazily, in a thread-safe
fashion.
> > The state is then reference-counted by the module un/initialisation
state,
> > so there are no dead-references.
> >
> >
> >
> >
> >
> >
> > "Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu21hi$1h9a$1@digitaldaemon.com...
> > > The best solution is to define a template which creates one instance,
> when
> > > this instance is requested. For example:
> > >
> > > template <class T> class Singleton {
> > >     private T *_object;
> > >     T *object() {
> > >         if (_object == null) _object = new T();
> > >         return _object;
> > >     }
> > > }
> > >
> > >
> > > "Phill" <phill@pacific.net.au> wrote in message news:bu20mp$1ft2$1@digitaldaemon.com...
> > > > You should be able to implement a Singleton pattern
> > > > easy.
> > > > This is what I do in Java
> > > >
> > > > public class One{
> > > >      public static One one;
> > > >
> > > > public static void getOne(){
> > > >         if(one == null){
> > > >               one new One();
> > > >             }
> > > >     }
> > > >
> > > > private One(){ // private constructor
> > > >    //construct stuff
> > > > }
> > > >
> > > > }
> > > >
> > > > This way if there is an instance of One already
> > > > you cannot make another One.
> > > >
> > > > This was good for me in Java because I found
> > > > that when clicking a JTable for some reason the
> > > > Thread goes through the event listener method
> > > > twice.
> > > >
> > > > The Singleton pattern solved this problem for
> > > > me.
> > > >
> > > >
> > > > Phill.
> > > >
> > > >
> > > >
> > > >
> > > > "imr1984" <imr1984_member@pathlink.com> wrote in message news:bu0da8$1qic$1@digitaldaemon.com...
> > > > > Often programmers make classes that are only intended to have one
> > > instance
> > > > for
> > > > > the duration of the whole program. These are called singleton
> classes.
> > D
> > > > should
> > > > > have built in support for them so that a singleton object will
have
> > its
> > > > own
> > > > > methods generated for it, without the use of a this pointer. This
> > would
> > > > save on
> > > > > a this parameter being pushed on stack to a singleton class
method.
> > > > >
> > > > > Walter, what da ya say?
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
> >
>
>


January 18, 2004
That's why there can be a synchronized singleton class:

template <class X> SynchronizeSingleton {

    private Mutex mutex = new Mutex();

    private X object = null;

    public X getObject() {
        mutex.lock();
        if (object == null) object = new X();
        mutex.unlock();
        return object;
    }
}

It's dead easy.

"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:bua4ih$2o0h$1@digitaldaemon.com...
> The threading problem's very simple: two or more threads may call object() for "the first time" at the same time.
>
> Q: how many instances of T get created?
>
>
>
> "Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu9lqn$20fo$1@digitaldaemon.com...
> > What thread nasties ? if you want to make a template synchronized class,
> go
> > ahead. Nothing stops you.
> >
> > As for the dead reference problems, since the internal pointer is never nullified, the object will not go away, unless the singleton class is destroyed. But singletons are supposed to be used that way.
> >
> > I don't see any of the problems you are mentioning.
> >
> > "Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:bu27c5$1q90$1@digitaldaemon.com...
> > > No. That leaves you with all the thread nasties and ordering and dead reference problems.
> > >
> > > A valid compromise is to have the singleton notionally initialised
> during
> > > module initialisation - a wonderful boon from D, and not to be
wasted -
> > and
> > > then to have any expensive state created lazily, in a thread-safe
> fashion.
> > > The state is then reference-counted by the module un/initialisation
> state,
> > > so there are no dead-references.
> > >
> > >
> > >
> > >
> > >
> > >
> > > "Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu21hi$1h9a$1@digitaldaemon.com...
> > > > The best solution is to define a template which creates one
instance,
> > when
> > > > this instance is requested. For example:
> > > >
> > > > template <class T> class Singleton {
> > > >     private T *_object;
> > > >     T *object() {
> > > >         if (_object == null) _object = new T();
> > > >         return _object;
> > > >     }
> > > > }
> > > >
> > > >
> > > > "Phill" <phill@pacific.net.au> wrote in message news:bu20mp$1ft2$1@digitaldaemon.com...
> > > > > You should be able to implement a Singleton pattern
> > > > > easy.
> > > > > This is what I do in Java
> > > > >
> > > > > public class One{
> > > > >      public static One one;
> > > > >
> > > > > public static void getOne(){
> > > > >         if(one == null){
> > > > >               one new One();
> > > > >             }
> > > > >     }
> > > > >
> > > > > private One(){ // private constructor
> > > > >    //construct stuff
> > > > > }
> > > > >
> > > > > }
> > > > >
> > > > > This way if there is an instance of One already
> > > > > you cannot make another One.
> > > > >
> > > > > This was good for me in Java because I found
> > > > > that when clicking a JTable for some reason the
> > > > > Thread goes through the event listener method
> > > > > twice.
> > > > >
> > > > > The Singleton pattern solved this problem for
> > > > > me.
> > > > >
> > > > >
> > > > > Phill.
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > "imr1984" <imr1984_member@pathlink.com> wrote in message news:bu0da8$1qic$1@digitaldaemon.com...
> > > > > > Often programmers make classes that are only intended to have
one
> > > > instance
> > > > > for
> > > > > > the duration of the whole program. These are called singleton
> > classes.
> > > D
> > > > > should
> > > > > > have built in support for them so that a singleton object will
> have
> > > its
> > > > > own
> > > > > > methods generated for it, without the use of a this pointer.
This
> > > would
> > > > > save on
> > > > > > a this parameter being pushed on stack to a singleton class
> method.
> > > > > >
> > > > > > Walter, what da ya say?
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> > >
> >
> >
>
>


January 22, 2004
Unfortunately that sprinkles code to create a new T all over your program, whereever it is used.

C++ sucks.

Sean

"Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu21hi$1h9a$1@digitaldaemon.com...
> The best solution is to define a template which creates one instance, when this instance is requested. For example:
>
> template <class T> class Singleton {
>     private T *_object;
>     T *object() {
>         if (_object == null) _object = new T();
>         return _object;
>     }
> }


January 22, 2004
>The best solution is to define a template which creates one instance, when this instance is requested. For example:
>
>template <class T> class Singleton {
>    private T *_object;
>    T *object() {
>        if (_object == null) _object = new T();
>        return _object;
>    }
>}
>

What's that? It isn't D, not C++ nor Java, nor C#. What is it?


January 24, 2004
"Matthias Becker" <Matthias_member@pathlink.com> wrote in message news:bup8jt$2vb8$1@digitaldaemon.com...
> >The best solution is to define a template which creates one instance,
when
> >this instance is requested. For example:
> >
> >template <class T> class Singleton {
> >    private T *_object;
> >    T *object() {
> >        if (_object == null) _object = new T();
> >        return _object;
> >    }
> >}
> >
>
> What's that? It isn't D, not C++ nor Java, nor C#. What is it?

It's D-T, the pseudo-code for D templates that a lot of us poor inculcated C++ template chappies use. Seems not in the least noteworthy to me.


January 24, 2004

"Sean L. Palmer" <palmer.sean@verizon.net> wrote in message news:bup6kr$2rv5$1@digitaldaemon.com...
> Unfortunately that sprinkles code to create a new T all over your program, whereever it is used.
>
> C++ sucks.

It's the implementation that sucks, not the language protocol. The language does not say that each time a template declaration is created, a new class is compiled. It may well have been one class throughout the compiling process, as long as the type is the same.

I don't see why D should be litterred with such petite details as singletons, when the language itself offers the most elegant mechanism for it.

>
> Sean
>
> "Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bu21hi$1h9a$1@digitaldaemon.com...
> > The best solution is to define a template which creates one instance,
when
> > this instance is requested. For example:
> >
> > template <class T> class Singleton {
> >     private T *_object;
> >     T *object() {
> >         if (_object == null) _object = new T();
> >         return _object;
> >     }
> > }
>
>


January 24, 2004
Pseudo C/D/Java/C#!!!

"Matthias Becker" <Matthias_member@pathlink.com> wrote in message news:bup8jt$2vb8$1@digitaldaemon.com...
> >The best solution is to define a template which creates one instance,
when
> >this instance is requested. For example:
> >
> >template <class T> class Singleton {
> >    private T *_object;
> >    T *object() {
> >        if (_object == null) _object = new T();
> >        return _object;
> >    }
> >}
> >
>
> What's that? It isn't D, not C++ nor Java, nor C#. What is it?
>
>


March 08, 2004
davepermen wrote:

[...]
> most stupid trend-programming-idioms.
[...]
> mySingleton.set(10);
> printf("%i" \n,mySingleton.get());

What do you call a program that allows a private variable ot be set as well as retrieved by getters and setters, that does not have any side effects, like for example writing into a protocol file, instead of making that variable global?

So long.
1 2 3
Next ›   Last »