View Antoni Milton's profile on LinkedIn

Thursday, February 4, 2010

Singleton Design Pattern with example C++ code

The Singleton design pattern ensures only one instance of a class in an application. For example, Interfaces object need by both GUI and Back-end class. But only one interface object enough for the whole application (GUI + interface + Back-end)

Here need to develop the interface class as a Singleton class.

static CInterface *s_ptrInterface = 0;

class CInterface
~ CInterface();
static CInterface* getInterface()
 lock(); // for thread safe.
if( 0 != s_ptrInterface )
s_ptrInterface = new CInterface();

return s_ptrInterface ;
Need to create the object for CInterface like below,
CInterface:: getInterface() // it’s always return the same object

No comments: