The Singleton design pattern

Hy there, here’s a singleton class(SoundManager). Using a singleton design pattern is very useful, at least that’s what i use in game developement.  This design pattern make your life easy beacause it has only one instance.. again .. ONLY ONE  :)  threfore you can call the static member function getInstance() to obtain the single instance of the class from anywhere in the code … enaugh said .. the rest is code :)

made just to prove the singleton pattern

 

#include <iostream>
using namespace std;
class Sound{};
class SoundManager
{
static SoundManager * instance;
static int numberOfInstances;
Sound * sounds;
int numberOfSounds;
SoundManager()
sounds = new Sound[]; // DYNAMIC ARRAY OF SOUNDS
numberOfSounds = 0;
};
public:
~SoundManager(){ numberOfInstances — ; /*IF WE DESTROY A INSTANCE OF THE SINGLETON WITH delete() WE MUST DECREMENT THE NUMBER OF INSTANCES*/};
static SoundManager * getInstance();
void addSound(Sound sound);
int howManySounds();
int howManyInstances();
};
int SoundManager::numberOfInstances = 0; // INITIALIZE THE NUMBER OF INSTANCES WITH 0
SoundManager* SoundManager::instance = NULL; // MAKE THE INITIAL INSTANCE NULL
SoundManager * SoundManager::getInstance()
{
if(numberOfInstances == 0) // IF THE NUMBER OF INSTANCES IS 0 (NONE HAS BEEN MADE YET)  … 
instance = new SoundManager(); // WE SHOULD MAKE A NEW ONE … RIGHT ? :)
numberOfInstances ++ ; // INCREMENT THE INSTANCE COUNTER
return instance;
}
// .. THE REST IS HISTORY
void SoundManager::addSound(Sound sound)
{
cout << ” – added sound – ” << endl;
sounds[numberOfSounds++] = sound;
}
int SoundManager::howManySounds()
{
return numberOfSounds;
}
int SoundManager::howManyInstances()
{
return numberOfInstances;
}
int main()
{
SoundManager *s1, *s2;
Sound s,a,b,c;
s1 = SoundManager::getInstance();
s2 = SoundManager::getInstance();
s1->addSound(s);
s1->addSound(a);
s1->addSound(b);
s1->addSound(c);
cout << ” No instances : ” << s1->howManyInstances() << endl; // WE NOW HAVE JUST 2 INSTANCES OF TRHE OBJECT BUT ONLY ONE OBJECT
cout << ” No sounds : ” << s2->howManySounds() << endl; // WE HAVE 4 SOUNDS
cout << ” No sounds : ” << s1->howManySounds() << endl; // STIL 4 SOUNDS BEACAUSE THE S1 POINTS AT THE SAME INSTANCE AS S2
// LET’S MAKE ANOTHER INSTANCE … BUT NOT A NEW OBJECT …
SoundManager *lastInstance = SoundManager::getInstance();
cout << ” No sounds : ” << lastInstance->howManySounds() << endl; // YEP STIL 4 SOUNDS :)
cout << ” No instances : ” << lastInstance->howManyInstances() << endl;
return 0;
}

#include <iostream>

using namespace std;

class Sound{};

 

class SoundManager

{

static SoundManager * instance;

static int numberOfInstances;

Sound * sounds;

int numberOfSounds;

SoundManager()

sounds = new Sound[]; // DYNAMIC ARRAY OF SOUNDS

numberOfSounds = 0;

};

public:

~SoundManager(){ numberOfInstances — ; /*IF WE DESTROY A REFERENCE TO THE SINGLETON WITH delete() WE MUST DECREMENT THE NUMBER OF REFERENCE */};

static SoundManager * getInstance();

void addSound(Sound sound);

int howManySounds();

int howManyInstances();

};

 

int SoundManager::numberOfInstances = 0; // INITIALIZE THE NUMBER OF INSTANCES WITH 0

SoundManager* SoundManager::instance = NULL; // MAKE THE INITIAL INSTANCE NULL

 

SoundManager * SoundManager::getInstance()

{

if(numberOfInstances == 0) // IF THE NUMBER OF REFS IS 0 (NONE HAS BEEN MADE YET)  … 

instance = new SoundManager(); // WE SHOULD MAKE A NEW ONE … RIGHT ? :)

numberOfInstances ++ ; // INCREMENT THE REFS COUNTER

return instance;

}

// .. THE REST IS HISTORY

void SoundManager::addSound(Sound sound)

{

cout << ” – added sound – ” << endl;

sounds[numberOfSounds++] = sound;

}

 

int SoundManager::howManySounds()

{

return numberOfSounds;

}

 

int SoundManager::howManyInstances()

{

return numberOfInstances;

}

int main()

{

SoundManager *s1, *s2;

Sound s,a,b,c;

s1 = SoundManager::getInstance();

s2 = SoundManager::getInstance();

s1->addSound(s);

s1->addSound(a);

s1->addSound(b);

s1->addSound(c);

 

cout << ” No instances : ” << s1->howManyInstances() << endl; // WE NOW HAVE JUST 2 POINTERS TO THE OBJECT BUT ONLY ONE OBJECT

 

cout << ” No sounds : ” << s2->howManySounds() << endl; // WE HAVE 4 SOUNDS

cout << ” No sounds : ” << s1->howManySounds() << endl; // STIL 4 SOUNDS BEACAUSE THE S1 POINTS AT THE SAME INSTANCE AS S2

 

// LET’S MAKE ANOTHER POINTER AT THE SINGLETON CLASS… BUT NOT A NEW OBJECT …

SoundManager *lastInstance = SoundManager::getInstance();

cout << ” No sounds : ” << lastInstance->howManySounds() << endl; // YEP STIL 4 SOUNDS :)

 

cout << ” No instances : ” << lastInstance->howManyInstances() << endl;

 

return 0;

}

Advertisement

~ by sinea on May 16, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.