Hi all,

I have been reading on the net searching for what others alternatives do i have besides singleton and i came across the following site:-
http://www.bigroom.co.uk/blog/better-without-singletons

There is a paragraph stating:-
To accomplish this(singleton effect), create the class as a normal, non-singleton, class and enforce the single-instance only behaviour elsewhere in the application. If you want a global point of access (the problems with globals described above will still occur), then you could use a factory to create the instance and reuse it in any code that requests it. If you don’t want the global point of access then you could create a single instance and pass it as an argument to all the classes and methods that need it.

I don't quite understand how to do it. Can someone further explain the paragraph? Coding examples would be appreciated.

Thanks in advance!

I've found an example on Wikipedia but the information was in Dutch, so I've translated the comments in the code:

Singleton pattern in C++

.h file

class CSingleton {
    protected: 
             CSingleton();
    public:
             virtual ~CSingleton();
             static CSingleton* Instance();
    private:
             static CSingleton* m_pSingleton;
};

.cpp file

//Define static to prevent from getting linker errors
CSingleton* CSingleton::m_pSingleton = NULL;
 
CSingleton::CSingleton() { }
CSingleton::~CSingleton() { }
 
CSingleton* CSingleton::Instance()
{
    if(m_pSingleton == NULL)
       m_pSingleton = new CSingleton;
 
    return m_pSingleton;
}

Hope this helps ! :)

Edit:: I also found a good Wikipedia description of singleton here ...

The paragraph is a bit nonsensical, where it suggests using a singleton factory as a workaround to using a singleton.

Yes, the solution is to make a single instance and pass it around. You can use the private and friend features of C++ to prevent people from creating second copies.

read the Wikipedia page too. There may be something new to get there too.
http://en.wikipedia.org/wiki/Singleton_pattern

but according to me what I learn , if you using the class in
non trival environment then therefore you have to also make
protected these things.
* copy constructor
* assignment operator
* default constructor.
these are known as magic three , all the C++ programmers should
knew about them.

The paragraph is a bit nonsensical, where it suggests using a singleton factory as a workaround to using a singleton.

Yes, the solution is to make a single instance and pass it around. You can use the private and friend features of C++ to prevent people from creating second copies.

Nop don't use the private there, there is a reason for this . Try to
google and understood it first.

The paragraph is a bit nonsensical, where it suggests using a singleton factory as a workaround to using a singleton.

True the applications and domains are overlaps each other. but
this is not always true. Abstract Factory is just an alternative way
to the composition and Inheritance. It introduced dynamic loose binding rather than inheritance's tight and static binding. In the static Abstract Factory method we can pass an argument to tell what type of object we need. that's similar to we instantiating a
subtype. However take a window class , if Abstract partten is used
we can pass parameaters to create a rounded window or rectangle window.

so they have distinict meaning , but in pratice we use both together in typically ( think if our constructor is taking arguments then that statement is correct ).

Always think that abstract factory is an alternative to the Inheritance like composite and decorator parttern. and we use
singleton to limit only to create a one instance. For a example
there is only one ip address for a computer. But if we use many
objects to keep it's virtual object then there will be data duplication.
think you'r listining to the port 80 . then other virtual objects should also have that state information (or should be acknowledged about ). for that purpose we limit it to one place.
It's main purpose is to stop the duplication of representing the real world objects and entities.

So besides Singleton, can i say that there isnt any other way to ensure exactly 1 instance is created for a class? I can't seems to think of other way to work around it...

Well any way you use to ensure that there's no more than 1 instance created for a class will be called a 'singleton', so by definition no.

This article has been dead for over six months. Start a new discussion instead.