单件模式(Singleton)
2011-12-06 00:32
405 查看
意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点。
常用c++实现方法:智能指针、静态成员变量、内嵌类、外部显式销毁!
1、智能指针
class CSingleton_1
{
public:
static CSingleton_1 * Instance()
{
if (!s_hInstance.get())
s_hInstance = std::auto_ptr<CSingleton_1>(new CSingleton_1);
return s_hInstance.get();
}
protected:
CSingleton_1();
private:
static std::auto_ptr<CSingleton_1> s_hInstance;
};
std::auto_ptr<CSingleton_1> CSingleton_1::s_hInstance(NULL);
2、静态成员变量
class CSingleton_2
{
public:
static CSingleton_2 * Instance()
{
static CSingleton_2 s;
return &s;
}
protected:
CSingleton_2();
};
3、内嵌类
class CSingleton_3
{
public:
static CSingleton_3 * Instance()
{
if (!s_hInstance)
s_hInstance = new CSingleton_3;
static CCleaner s_cleaner;
return s_hInstance;
}
protected:
CSingleton_3();
private:
class CCleaner
{
public:
~CCleaner()
{
if (CSingleton_3::Instance())
delete CSingleton_3::Instance();
}
};
static CSingleton_3 * s_hInstance;
//static CCleaner s_cleaner;
};
CSingleton_3* CSingleton_3::s_hInstance = NULL;
//CSingleton_3::CCleaner CSingleton_3::s_cleaner;
4、外部显式销毁
class CSingleton_4
{
public:
static CSingleton_4 * Instance()
if (!s_hInstance)
s_hInstance = new CSingleton_4;
return s_hInstance;
}
void Destroy()
{
if (s_hInstance)
{
delete s_hInstance;
s_hInstance = NULL;
}
}
protected:
CSingleton_4();
private:
static CSingleton_4 * s_hInstance;
};
CSingleton_4* CSingleton_4::s_hInstance = NULL;
常用c++实现方法:智能指针、静态成员变量、内嵌类、外部显式销毁!
1、智能指针
class CSingleton_1
{
public:
static CSingleton_1 * Instance()
{
if (!s_hInstance.get())
s_hInstance = std::auto_ptr<CSingleton_1>(new CSingleton_1);
return s_hInstance.get();
}
protected:
CSingleton_1();
private:
static std::auto_ptr<CSingleton_1> s_hInstance;
};
std::auto_ptr<CSingleton_1> CSingleton_1::s_hInstance(NULL);
2、静态成员变量
class CSingleton_2
{
public:
static CSingleton_2 * Instance()
{
static CSingleton_2 s;
return &s;
}
protected:
CSingleton_2();
};
3、内嵌类
class CSingleton_3
{
public:
static CSingleton_3 * Instance()
{
if (!s_hInstance)
s_hInstance = new CSingleton_3;
static CCleaner s_cleaner;
return s_hInstance;
}
protected:
CSingleton_3();
private:
class CCleaner
{
public:
~CCleaner()
{
if (CSingleton_3::Instance())
delete CSingleton_3::Instance();
}
};
static CSingleton_3 * s_hInstance;
//static CCleaner s_cleaner;
};
CSingleton_3* CSingleton_3::s_hInstance = NULL;
//CSingleton_3::CCleaner CSingleton_3::s_cleaner;
4、外部显式销毁
class CSingleton_4
{
public:
static CSingleton_4 * Instance()
if (!s_hInstance)
s_hInstance = new CSingleton_4;
return s_hInstance;
}
void Destroy()
{
if (s_hInstance)
{
delete s_hInstance;
s_hInstance = NULL;
}
}
protected:
CSingleton_4();
private:
static CSingleton_4 * s_hInstance;
};
CSingleton_4* CSingleton_4::s_hInstance = NULL;
相关文章推荐
- 设计模式之单件模式(Singleton)
- [设计模式]SINGLETON(单件)_对象创建型模式
- C#面向对象设计模式纵横谈2 Singleton单件创建型模式
- C# 设计模式-单件(Singleton)
- 单件模式(Singleton)
- 设计模式学习笔记(二)——Singleton单件模式
- 单件模式-Singleton
- C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式)
- Singleton(单件)- 对象创建型模式
- Singleton 单件模式及其变体 Double-Checked Locking 双重检查锁模式
- 魔兽争霸之PHP设计模式-单件模式[Singleton]
- 设计模式--创建型-Singleton(单例单件)
- Singleton 单件模式及其变体 Double-Checked Locking 双重检查锁模式
- PHP设计模式之单件(Singleton)
- 设计模式之单件模式(Singleton)
- 单件模式 Singleton
- 设计模式学习笔记(二)——Singleton单件模式
- 【剑指offer】题2:实现单件(singleton)模式
- Singleton单件模式的思考(续)
- (创建型模式)Singleton 单件模式