单件模式(Singleton Pattern)创建型模式
定义:确保一个类只有一个实例,并提供全局访问点。
用来创建独一无二,只能有一个实例的对象的入场券。它是所有设计模式类图中最简单的。该模式在开发中非常有用。
实现单件模式需要私有的构造器、一个静态的方法和一个静态的变量。
该模式通常与抽象工厂模式或工厂模式配合使用,因为我们只需要一个工厂来创建对象。
该模式最简单,C++的实现如下:
Singleton.h头文件如下:
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();//该函数最好返回对象的引用而非指针,如果为指针,调用端有可能将它delete掉,传回reference更安全些。
protected:
Singleton(); //或者声明为private
Singleton(const Singleton&);//copy构造函数防止客户端创建一个单件的副本
Singleton& operator=(const Singleton&);//不能将唯一的Singleton对象赋予给另外一个对象,因为不可能存在两个Singleton对象。故需将赋值禁止。
static Singleton* _instance;
};
#endif
Singleton.CPP源文件
#include"Singleton.h"
#include <iostream>
using namespace std;
Singleton * Singleton::_instance=0;
Singleton::Singleton(const Singleton &rhs){}
Singleton::Singleton()
{
cout<<"Singleton....."<<endl;
}
Singleton * Singleton::Instance()
{
if(_instance==0)
_instance=new Singleton();
return _instance;
}
主函数main.CPP
#include"Singleton.h"
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
Singleton * sgn = Singleton::Instance();
return 0;
}
单件模式的一些问题:
1.销毁Singleton
Singleton是在被需求时才产生的,也就是Instance()第一次被调用时产生出来的,因此确定了构造时间,但是析构问题没有解决:Singleton应该在什么时候销毁自身的实体呢?实际上Singleton未被销毁也不会造成内存泄露,然后会造成资源泄露。这是因为Singleton构造函数可以索求广泛的资源:网络连接、OS互斥体(mutexes)和进程通讯方法中的各种handles等待。避免资源泄露的唯一正确做法就是程序结束或关闭时候删除Singleton对象,必须谨慎选择删除时机,确保Singleton对象被推毁后不会在被使用。
推毁Singleton的最简单的方法是依赖语言机制。如下:
Singleton & Singleton::Instance()
{
static Singleton obj; //函数内的static对象在该函数第一次执行时被初始化。
return obj;
}
int Fun()
{
static int x=100; //通过编译器常量加以初始化,Fun()第一次被调用前,x早就被设为100了。
return ++x;
}
2.无效Singleton对象引用问题
如果程序中使用多个相关联的Singletons,就无法提供某种自动方法来控制他们的寿命。一个合理的Singleton至少应该执行dead-reference 检测。为了做到这一点,可以声明一个成员变量static
bool _destoryed来追踪析构行为,初始值为false.析构函数将它设为真。
void Singleton::OnDeadReference()
{
throw std::runtime_error("无效引用");
}
3.让推毁的Singleton对象复活
怎么样让推毁的Singleton对象复活并且保证任何时候Singleton对象只有唯一的实体。如果检测到dead reference,就可以重新建立该实体(C++保证这种功能,因为静态对象的内存在整个程序生命期间都会保留着)。我们可以通过atexit()登记这个新对象的析构函数。
class Singleton
{
void KillSingleton();
}
void Singleton::OnDeadReference()
{
Create();
new(_instance) Singleton;
atexit(KillSingleton);
Destoryed=false;
}
void Singleton::KillSingleton()
{
_instance->~Singleton();
}
4.实现修改后的单件模式:
PhSingleton.h头文件
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton& Instance();
protected:
Singleton(); //或者声明为privated
Singleton(const Singleton &);
Singleton &operator=(const Singleton&);
virtual ~Singleton(); //声明为保护的,拥有Singleton对象指针者就不会意外删除
private:
static void Create();
static void OnDeadReference();
static void KillSingleton();
static Singleton* _instance;
static bool _destoryed;
};
#endif
PhSingleton.CPP源文件
#include"PhSingleton.h"
#include <iostream>
using namespace std;
Singleton * Singleton::_instance=0;
bool Singleton::_destoryed=false;
Singleton::Singleton()
{
cout<<"PhSingleton....."<<endl;
}
Singleton& Singleton::Instance()
{
if(!_instance)
{
if(_destoryed) //判断是否销毁对象
{
OnDeadReference(); //无效引用
}
else
{
Create();//第一次调用创建
}
}
return *_instance;
}
void Singleton::Create()
{
//初始化_instance
static Singleton theInstance;
_instance=&theInstance;
}
void Singleton::KillSingleton()
{
_instance->~Singleton();
}
void Singleton::OnDeadReference()
{
//throw std::runtime_error("无效引用");
Create();
new(_instance) Singleton; //在原来的地址创建一个Singleton,手工建造,手工销毁
atexit(KillSingleton);//atexit()由标准C程序库提供,注册一些在程序结束之际自动被调用的函数
//调用次序为后进先出LIFO;new和delete管理的对象不遵守这个规则。
_destoryed=false;
}
Singleton::~Singleton()
{
_instance=0;
_destoryed=true;
}
主调函数:main.cpp
#include "PhSingleton.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
Singleton *sgn =&Singleton::Instance();
Singleton *sgn1=&Singleton::Instance();
if(sgn==sgn1)
cout<<"The same instance..."<<endl;
/*if(sgn)
delete sgn;*///出现无法访问 protected 成员ingleton::~Singleton:
return 0;
}
上面单件模式的事件流程:程序结束过程中,Singleton析构函数被调用,于是将指针重置为0并将_destoryed设置为true;假设某个全局对象想再次使用Singleton,于是Instance()调用OnDeadReference (),后缀使Singleton复活,并注册KillSingleton(),然后Instance()成功返回一个合法的reference指向重生的Singleton对象。