×
Namespaces

Variants
Actions

How to create singleton class in Symbian

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: savaj (07 Mar 2009)
Last edited: hamishwillee (26 Jul 2012)

Contents

What is Singleton class?

A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist.

How to implement it in symbian?

Symbian provided CCoeStatic base class for creating singleton class. To omplement singleton pattern you have to derive your class from CCoeStatic class.

Header file

//MySingleton.h
#include <coemain.h>
 
class CMySingleton: public CCoeStatic
{
public:
static CMySingleton* GetInstance();
~CMySingleton();
 
private:
CMySingleton();
void InitializeL();
public:
void UpdateData(const TDesC8& aText);
TInt iNumber;
}

Implementation file.

//MySingleton.cpp
static const TUid KUidMySingleton = { 0x10000123 };
 
CMySingleton* CMySingleton::GetInstance()
{
CMySingleton* pInstance = (CMySingleton*)CCoeEnv::Static(KUidMySingleton);
if (!pInstance)
{
pInstance = new (ELeave) CMySingleton();
pInstance->InitializeL();
}
 
return pInstance;
}
 
CMySingleton::~CMySingleton()
{
}
 
CMySingleton::CMySingleton():CCoeStatic(KUidMySingleton)
{
}
 
void CMySingleton::InitializeL()
{
iNumber = 8; //initialize data member here.
}
 
void CMySingleton::UpdateData(const TDesC8& aText)
{
}


How to use it?

Add header file mysingleton.h in your class. And use it in following manner.
TBuf8<20> data( _L8("singleton") );
CMySingleton* pMySingleton = CMySingleton::GetInstance(); //if instance of class is available then it return it, else it will create new instance
pMySingleton->UpdateData(data)
This page was last modified on 26 July 2012, at 02:10.
36 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×