意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点。
主要解决:一个全局使用的类频繁地创建与销毁。
何时使用:当您想控制实例数目,节省系统资源的时候。
如何解决:判断系统是否已经有这个单例,如果有则返回,如果没有则创建。
关键代码:构造函数是私有的。
应用实例: 1、一个党只能有一个书记。 2、Windows 是多进程多线程的,在操作一个文件的时候,就不可避免地出现多个进程或线程同时操作一个文件的现象,所以所有文件的处理必须通过唯一的实例来进行。 3、一些设备管理器常常设计为单例模式,比如一个电脑有两台打印机,在输出的时候就要处理不能两台打印机打印同一个文件。
优点: 1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。 2、避免对资源的多重占用(比如写文件操作)。
缺点:没有接口,不能继承,与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。
使用场景: 1、要求生产唯一序列号。 2、WEB 中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来。 3、创建的一个对象需要消耗的资源过多,比如 I/O 与数据库的连接等。
注意事项:getInstance() 方法中需要使用同步锁 synchronized (Singleton.class) 防止多线程同时进入造成 instance 被多次实例化。
单例模式又分为(饿汉模式,懒汉模式)两种:
饿汉模式:程序运行即创建对象并实例化,静态实现所以线程是安全的。 以时间换空间
懒汉模式:创建对象不实例化,需要的时候才实例化,线程不安全需要(加锁)。 以空间换时间
饿汉代码:
/*
* design.hpp
*
* Created on: 2019年7月12日
* Author: izhang
*/
#pragma once
#include <iostream>
using namespace std;
class Singleton
{
private:
Singleton();
~Singleton();
static Singleton* instance; //这是我们的单例对象,它是一个类对象的指针
public:
static Singleton* getInstance();
static void deleteInstance();
private:
//定义一个内部类
class Garbo
{
public:
Garbo(){}
~Garbo()
{
if (NULL != instance)
{
delete instance;
instance = NULL;
}
}
};
//定义一个内部类的静态对象
//当该对象销毁的时候,调用析构函数顺便销毁我们的单例对象
static Garbo _garbo;
};
//============================================================================
// Name : design.cpp
// Author : ice
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "design.hpp"
Singleton::Singleton()
{
cout<<"Create a Singleton"<<endl;
}
Singleton::~Singleton()
{
cout<<"destroy a Singleton"<<endl;
}
Singleton* Singleton::getInstance()
{
return instance;
}
void Singleton::deleteInstance()
{
delete instance;
}
//下面这个静态成员变量在类加载的时候就已经初始化好了
Singleton* Singleton::instance = new Singleton();
Singleton::Garbo Singleton::_garbo;
/*
* main.cpp
*
* Created on: 2019年7月12日
* Author: izhang
*/
#include "design.hpp"
int main()
{
Singleton* test1 = Singleton::getInstance();
Singleton* test2 = Singleton::getInstance();
if (test1 == test2)
{
cout<<"This is a real Singleton!"<<endl;
}
else
{
cout<<"Fake Singleton?"<<endl;
}
// Singleton::deleteInstance(); //手动调太傻了...换一种方式
return 0;
}
全局数据区中,存储的并不是一个实例对象,而是一个实例对象的指针,它是一个地址而已,我们真正占有资源的实例对象是存储在堆中的。这样的声明方法可以减小全局数据区的占用量,把一大堆单例对象放在了堆中,但我们需要主动地去调用delete释放申请的资源。或者,就同上述程序中一样定义一内部类,定义一个内部类的静态对象,当该对象销毁的时候,调用析构函数顺便销毁我们的单例对象。详情参考:https://blog.csdn.net/lvyibin890/article/details/81943637
懒汉代码:
其余都一样,仅getInstance和instance静态成员变量初始化有区别。
//============================================================================
// Name : design.cpp
// Author : ice
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "design.hpp"
Singleton::Singleton()
{
cout<<"Create a Singleton"<<endl;
}
Singleton::~Singleton()
{
cout<<"destroy a Singleton"<<endl;
}
//懒汉模式主要区别在这里
Singleton* Singleton::getInstance()
{
if (NULL == instance)
{
Lock(); //借用其它类来实现,如boost\pthread_mutex_t类
if (NULL == instance)
instance = new Singleton();
Unlock();
}
return instance;
}
void Singleton::deleteInstance()
{
delete instance;
}
//下面这个静态成员变量在类加载的时候就已经初始化好了
Singleton* Singleton::instance = NULL;
Singleton::Garbo Singleton::_garbo;