C++
饿汉式
template<typename T>
class Singleton
{
private:
//隐藏构造函数、析构函数,删除拷贝构造函数、赋值运算符
Singleton()
{
cout << "程序运行时就打印该信息" << endl;
}
~Singleton() {}//该类不能在外被析构
Singleton(const Singleton&) = delete;//该类不能在外被拷贝
Singleton& operator=(const Singleton&) = delete;//该类不能在外被赋值
static Singleton data;//该进程下唯一Singleton对象,即单例
public:
static Singleton* GetInstance()//外部只能通过调用该函数拿到data
{
return &data;
}
};
template<typename T>
Singleton<T> Singleton<T>::data{}; //Singleton<T> Singleton<T>::data = Singleton<T>();
懒汉式
线程不安全
template<typename T>
class Singleton
{
private:
//隐藏构造函数、析构函数,删除拷贝构造函数、赋值运算符
Singleton()
{
cout << "第一次GetInstance才打印该信息" << endl;
}
~Singleton() {} //该类不能在外被析构
Singleton(const Singleton&) = delete; //该类不能在外被拷贝
Singleton& operator=(const Singleton&) = delete; //该类不能在外被赋值
static Singleton* _instance; //使用Singleton*作为访问唯一对象的入口
public:
static Singleton* GetInstance() //外部只能通过调用该函数拿到instance
{
if (_instance == nullptr)
{
_instance = new Singleton();
}
return _instance;
}
};
template<typename T>
Singleton<T>* Singleton<T>::_instance = nullptr; // _instance的初始化
加锁
#include <iostream>
#include <mutex>
class Singleton {
private:
// 私有静态指针变量,指向类的唯一实例
static Singleton* instance;
// 私有静态互斥锁
static std::mutex mutex;
// 私有构造函数
Singleton() { }
~Singleton() { } //该类不能在外被析构
// 禁止复制构造函数和赋值操作符
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
public:
// 公有的静态方法,用于获取类的唯一实例
static Singleton* getInstance() {
// 局部静态变量,确保只创建一个实例
if (instance == nullptr) {
std::lock_guard<std::mutex> lock(mutex);
if (instance == nullptr) {
instance = new Singleton();
}
}
return instance;
}
};
// 初始化静态成员变量
Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mutex;
int main() {
// 获取单例实例并调用其方法
Singleton::getInstance()->doSomething();
return 0;
}
template<typename T>
class Singleton
{
private:
//隐藏构造函数、析构函数,删除拷贝构造函数、赋值运算符
Singleton()
{
cout << "第一次GetInstance才打印该信息" << endl;
}
~Singleton() {} //该类不能在外被析构
Singleton(const Singleton&) = delete; //该类不能在外被拷贝
Singleton& operator=(const Singleton&) = delete; //该类不能在外被赋值
static Singleton* _instance; //使用Singleton*作为访问唯一对象的入口
static pthread_mutex_t _lock; //保证线程安全
public:
static Singleton* GetInstance() //外部只能通过调用该函数拿到instance
{
if (_instance == nullptr)
{
pthread_mutex_lock(&lock_);
if (_instance == nullptr) _instance = new Singleton();
pthread_mutex_unlock(&lock_);
}
return _instance;
}
};
template<typename T>
Singleton<T>* Singleton<T>::_instance = nullptr; // _instance的初始化
template<typename T>
pthread_mutex_t Singleton<T>::_lock = PTHREAD_MUTEX_INITIALIZER;//锁的初始化
使用局部静态变量
template<typename T>
class Singleton
{
private:
Singleton()
{
cout << "construct" << endl;
}
~Singleton() {
cout << "destruct" << endl;
}
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
public:
static Singleton* GetInstance()
{
static Singleton _instance;
return &_instance;
}
};
C++11保证静态变量初始化的线程安全
C#
public class Singleton
{
private static Singleton? _instance;
private static readonly object _syncRoot = new object();
private Singleton() { }
public static Singleton GetInstance()
{
lock (_syncRoot)
{
if (_instance == null)
_instance = new Singleton();
}
return _instance;
}
}
Unity
private static Singleton<T> instance;
public static Singleton<T> Instance{
get
{
if (instance == null)
//instance = FindObjectOfType<UIController>();
instance = GameObject.Instantiate<Singleton<T>>();
return instance;
}
}
private void Awake()
{
if (instance == null) instance = this;
else Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}