单例模式
为什么要用单例
确保某个类只有一个对象,常用于访问数据库操作,服务的配置文件等。
单例的关键点
1、默认构造函数为private,复制构造函数和复制赋值函数也要private或=delete禁用。(做到无法被外部其他对象构造) 2、通过一个静态方法或枚举返回单例类对象。 3、确保多线程的环境下,单例类对象只有一个。
几种写法
本文主要介绍C++的懒汉式和饿汉式写法。
懒汉式
需要生成唯一对象时(调用GetInstance时),才生成
线程不安全的错误写法
class SingleInstance
{
public:
static SingleInstance *GetInstance();
static void deleteInstance();
private:
SingleInstance() {}
~SingleInstance() {}
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
private:
static SingleInstance *m_SingleInstance;
};
SingleInstance *SingleInstance::m_SingleInstance = NULL;
SingleInstance* SingleInstance::GetInstance()
{
if (m_SingleInstance == NULL)
{
m_SingleInstance = new (std::nothrow) SingleInstance;
}
return m_SingleInstance;
}
void SingleInstance::deleteInstance()
{
if (m_SingleInstance)
{
delete m_SingleInstance;
m_SingleInstance = NULL;
}
}
线程安全的双检锁写法
class SingleInstance
{
public:
static SingleInstance *GetInstance();
static void deleteInstance();
private:
SingleInstance() {}
~SingleInstance() {}
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
private:
static SingleInstance *m_SingleInstance;
};
SingleInstance *SingleInstance::m_SingleInstance = NULL;
SingleInstance* SingleInstance::GetInstance()
{
if (m_SingleInstance == NULL)
{
std::unique_lock<std::mutex> lock(m_Mutex);
if (m_SingleInstance == NULL)
{
m_SingleInstance = new (std::nothrow) SingleInstance;
}
}
return m_SingleInstance;
}
void SingleInstance::deleteInstance()
{
if (m_SingleInstance)
{
delete m_SingleInstance;
m_SingleInstance = NULL;
}
}
线程安全的局部静态变量写法
推荐
class SingleInstance
{
public:
static SingleInstance *GetInstance();
private:
SingleInstance() {}
~SingleInstance() {}
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
};
SingleInstance& SingleInstance::GetInstance()
{
static SingleInstance m_SingleInstance;
return m_SingleInstance;
}
饿汉式
进程运行前(main函数执行),就创建
线程安全的进程运行前初始化写法
class SingleInstance
{
public:
static SingleInstance *GetInstance();
private:
SingleInstance() {}
~SingleInstance() {}
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
private:
static SingleInstance *m_SingleInstance;
};
SingleInstance* SingleInstance::GetInstance()
{
return m_SingleInstance;
}
Singleton* Singleton::g_pSingleton = new (std::nothrow) Singleton;
int main()
{
return 0;
}
线程安全的类静态成员变量写法
class SingleInstance
{
public:
static SingleInstance *GetInstance();
static SingleInstance m_SingleInstance;
private:
SingleInstance() {}
~SingleInstance() {}
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
};
SingleInstance& SingleInstance::GetInstance()
{
return m_SingleInstance;
}
静态内部类写法
JAVA
public class Singleton {
private Singleton() {
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
private static class SingletonHolder {
private static Singleton instance = new Singleton();
}
}
第一次加载Singleton类时不会初始化instance,只有在第一次调用getInstance()方法时,虚拟机会加载SingletonHolder类,初始化instance。 这种方式既保证线程安全,单例对象的唯一,也延迟了单例的初始化,推荐使用这种方式来实现单例模式。
枚举单例
JAVA
public enum SingletonEnum {
INSTANCE;
public void doSomething() {
System.out.println("do something");
}
}
默认枚举实例的创建是线程安全的,即使反序列化也不会生成新的实例,任何情况下都是一个单例。 优点: 简单!
容器实现单例
JAVA
import java.util.HashMap;
import java.util.Map;
public class SingletonManager {
private static Map<String, Object> objMap = new HashMap<String, Object>();
public static void regsiterService(String key, Object instance) {
if (!objMap.containsKey(key)) {
objMap.put(key, instance);
}
}
public static Object getService(String key) {
return objMap.get(key);
}
}
SingletonManager可以管理多个单例类型,使用时根据key获取对象对应类型的对象。这种方式可以通过统一的接口获取操作,隐藏了具体实现,降低了耦合度。
参考
https://www.jianshu.com/p/e57098c265f9 https://www.cnblogs.com/kubixuesheng/p/4355055.html https://stackoverflow.com/questions/1008019/c-singleton-design-pattern https://programmer.ink/think/summary-of-c-thread-safety-singleton-patterns.html
|