Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Singleton pattern for using variable anywhere in the code just like global variable

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: kkrish (10 Jul 2009)
Last edited: hamishwillee (26 Jul 2012)

Warning

This code will create issues when used in a DLL as it uses writable static data. Though WSDs' are supported from EKA2 onwards but gcce compiler provided with public sdk's has known defects that prevent reliable support for this. Only Arm compilers (device target) are known to provide a reliable support. However if you intend to use this code in a dll make sure that you add EPOCALLOWDLLDATA statment to your mmp file otherwise the code will not link for the device target.



In any programming language, there are various ways to save data as we also have in symbian. We can also create our user define data type in the form of structure, union and array etc, but the main problem occur when we need to some data from in all the classes let suppose, the first screen of the application need user name and password and if user input the correct values in the boxes might we need the same values in further classed, might be for authentication etc.

The problem is small if we have two classes then we can easily handle it by just passing the argument from one class to another, but what happen if there are more 50 classes in the project and when ever you create the instance you need to supply the argument, whether they might be need or not at that moment and it will reduce the performance of your application.


The easiest way to achieve this to keep your member variable/ data into AppUi class as public or you can also define public method for get or set the value for the data member. This is the right way to achieve this, but if you project is in developing stage and you could not figure out the number of variable required at that time in future the size of AppUi class will increase. So I always prefer to create separate singleton class for it.


Class declaration…

class CGlobalData : public CBase //make a singleton class
{
public:
~CGlobalData();
static CGlobalData* NewL();
static CGlobalData* NewLC();
 
private:
CGlobalData();
void ConstructL();
 
public:
TBool InstanceExist();
CGlobalData* GetGlobalDataInstance();
 
 
TDes& GetUserName();
void SetUserName(const TDesC& aName);
 
TDes& GetPassword();
void SetPassword(const TDesC& aPassword);
 
private:
TBuf<64> iName;
TBuf<32> iPassword;
};

Class definition….

static CGlobalData* iGlobalData = NULL;
CGlobalData::CGlobalData()
{
}
CGlobalData::~CGlobalData()
{
}
CGlobalData* CGlobalData::NewLC()
{
if(iGlobalData == NULL)
{
iGlobalData = new (ELeave) CGlobalData();
CleanupStack::PushL(iGlobalData);
iGlobalData->ConstructL();
}
return iGlobalData ;
 
}
CGlobalData* CGlobalData::NewL()
{
if(iGlobalData == NULL)
{
iGlobalData = CGlobalData::NewLC();
CleanupStack::Pop(); // self;
}
return iGlobalData;
}
void CGlobalData::ConstructL()
{
}
TDes& CGlobalData::GetUserName()
{
return iName;
}
void CGlobalData::SetUserName(const TDesC& aName)
{
iName.Copy(aName);
}
TDes& CGlobalData::GetPassword()
{
return iPassword;
}
void CGlobalData::SetPassword(const TDesC& aPassword)
{
iPassword.Copy(aPassword);
}
TBool CGlobalData :: InstanceExist()
{
if (iGlobalData)
return ETrue;
else
return EFalse;
 
}
CGlobalData* GetGlobalDataInstance()
{
return iGlobalData;
}

Now the instance of the class is created only once and you can create it anywhere in the code. When ever you will try to create an instance you will the get the pointer of already created instance.

e.g. First time call for the class CGlobalData.

CGlobalData* globalData = CGlobalData::NewL();
globalData->SetUserName(_L("UserOne"));
globalData->SetPassword(_L("PasswordOne"));

The above lines called first time so instance of CGlobalData class will be created.

Second time call:

static CGlobalData* gd = CGlobalData::NewL();
TBuf<64> name;
TBuf<64> password;
name.Copy(gd->GetUserName());
password.Copy(gd->GetPassword());

In this case the NewL function will return the pointer of already created CGlobalData object so name and password variable will contains UserOne and PasswordOne respectively.

Related links

This page was last modified on 26 July 2012, at 05:06.
21 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.

×