How to create singleton
来源:互联网 发布:数据预测模型算法 编辑:程序博客网 时间:2024/05/12 20:33
TITLe:How to create singleton class in Symbian
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.
for example:
//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;
}
//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)
{
}
you can make to a temple
template <class aType, TInt aUid> classCMySingleton: public CCoeStatic
{
TYPE iInstance;
CSingleton() : CCoeStatic( TUid::Uid(aUid) ) {}
public:
/**
Answer a location to hold a static variable of type TYPE.
UID must be a unique ID (preferably allocated by Symbian).
*/
static TYPE& GetL()
{
CSingleton<aType, aUid>* staticData = static_cast<CSingleton<aType, aUid>* >(CCoeEnv::Static(TUid::Uid(aUid)));
if (staticData)
return staticData->iInstance;
//CCoeStatic会处理内存释放的问题
staticData = new (ELeave) CMySingleton<aType, aUid>;
return staticData->iInstance;
}
};
you can use as follow:
const TInt KASingletomUid = 1;
class A;
CMySingleton<A, KASingletomUid > ASigleton;
ASigleton::GetL();
- How to create singleton
- [Ruby]How to create singleton class ?
- how to create a global singleton object in exe
- How to create alarm
- How to create WCF
- How to Create Tables
- Siebel HOW TO: How to create Products?
- How to Create a Customer
- How to create Firefox extensions
- How to create new geodatabases
- how to create cifs.ko
- How to create EPS files...
- How to create PFX file
- how to create a Makefile
- [AngularJS] How to create Directives?
- How to create recovery image
- how to create proxies pool
- How to create xorg.conf
- RDS-TMC(Traffic Message Channel)蕴藏的商机不可小视
- Delphi图像处理 -- 平面几何变换(上)
- java时间的处理
- 利用JS获取传过来的参数
- 链表
- How to create singleton
- VMware Workstation下安装Linux系统
- 在Oracle中存储与管理大对象数据类型
- 那一天 那一月 那一年 那一世
- Gtk+/Cairo/Glitz扯开漫谈
- 华为 U2000 技术研究开始
- Tips of Scurm
- Microsoft Virtual Server 2005 入门篇
- 选择三层实木复合地板 摆脱地板变形厄运