設計模式(4)-序列生成器之單例模式
場景:序列生成器
系統中統一的序列生成程式,整個系統統一一套!那麼就用單例模式吧!
首先看看單例模式
1)類持有一個自己的例項,而且還是個靜態例項。
2)類的建構函式為私有屬性。
3)用以獲得例項的方法為靜態方法。
看看類圖
然後看一段試例程式:
#include <iostream> using namespace std; class Singleton{ private: Singleton();//注意:構造方法私有 virtual ~Singleton(); static Singleton* instance;//惟一例項 int var;//成員變數(用於測試) public: static Singleton* GetInstance();//工廠方法(用來獲得例項) int getVar();//獲得var的值 void setVar(int);//設定var的值 }; //構造方法實現 Singleton::Singleton() { this->var = 20; cout<<"Singleton Constructor"<<endl; } Singleton::~Singleton() { if(instance != NULL) { delete instance; } } //初始化靜態成員 //Singleton* Singleton::instance=new Singleton(); Singleton* Singleton::instance=NULL; Singleton* Singleton::GetInstance() { if(instance == NULL) { instance = new Singleton(); } return instance; } //seter && getter含數 int Singleton::getVar() { return this->var; } void Singleton::setVar(int var) { this->var = var; } int main(int argc, char* argv[]) { Singleton *ton1 = Singleton::GetInstance(); Singleton *ton2 = Singleton::GetInstance(); cout<<"ton1 var = "<<ton1->getVar()<<endl; ton1->setVar(150); cout<<"ton2 var = "<<ton2->getVar()<<endl; return 0; }
1、構造方法私有
那麼,就意味著,只能在Singleton的成員函式中,才能呼叫Singleton的建構函式來建立例項。在Singleton之外,不能建立Singleton物件的例項。
2、程式碼中,定義了GetInstance方法,只能通過GetInstance方法來獲取Singleton物件的例項,單例就是在GetInstance方法中控制的。
首先,Singleton有一個 static Singleton* instance;//惟一例項
Singleton* Singleton::instance=NULL; 在這裡初始化為NULL。
Singleton* Singleton::GetInstance() { if(instance == NULL) { instance = new Singleton(); } return instance; }
上面的函式,就是通過instance來實現單例的。
當第一次呼叫GetInstance時,instance 為NULL,所以會執行 instance = new Singleton(); 把這個新建的例項儲存到靜態成員instance,並返回這個指標。
第二次到第N次呼叫GetInstance時,由於instance不為空,所以會直接返回instance 。也就是第一次呼叫GetInstance建立的那個例項。
所以這樣就實現了,單例項。
意思就是說,Singleton物件的例項,只會被建立一次,就是說記憶體中,只存在一個Singleton的例項,就是所謂,單例項。
弄個生成單例的例項程式吧!
#include <sys/sem.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <iostream>
using namespace std;
#define MAXID 9999
static struct sembuf op_open={1,-1,0 };
class GenHH{
private:
GenHH();//注意:構造方法私有
virtual ~GenHH();
static GenHH* instance;//惟一例項
int opensem(key_t semkey);
int creatsem(key_t semkey,int bigcount);
int sem_open(int semid);
unsigned int gen_seq();
public:
static GenHH* getInstance();//工廠方法(用來獲得例項)
unsigned int gen_hh();
}
GenHH::~GenHH()
{
if(instance != NULL)
{
delete instance;
}
}
//初始化靜態成員
GenHH* GenHH::instance=NULL;
GenHH* GenHH::getInstance()
{
if(instance == NULL)
{
instance = new Singleton();
}
return instance;
}
unsigned int GenHH::gen_hh()
{
unsigned int hh;
char chh[9];
memset(chh,0,9);
sprintf(chh,"%05d%04d",time(NULL)%100000,gen_seq());
hh = atoi(chh);
return hh;
}
unsigned int GenHH::gen_seq()
{
int seq,kid;
int semid,semval;
struct timeval tv;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} semctl_arg;
kid=ftok("/etc/hosts",'m');
if(kid<0){
printf("system Error! Can't find /etc/hosts!n");
gettimeofday(&tv, NULL);
return tv.tv_usec % MAXID ;
}
semid=opensem(kid);
if(semid<=0){
semid=creatsem(kid,MAXID);
if(semid<0){
gettimeofday(&tv, NULL);
return tv.tv_usec % MAXID ;
}
}
semval=semctl(semid,1,GETVAL,0);
if(semval<=2){
semctl_arg.val=MAXID;
if ((semctl(semid,1,SETVAL,semctl_arg)) < 0 ){
gettimeofday(&tv, NULL);
return tv.tv_usec % MAXID ;
}
}
sem_open(semid);
semval=semctl(semid,1,GETVAL,0);
return MAXID-semval;
}
int GenHH::opensem(key_t semkey)
{
int semid;
semid=semget(semkey,2,0);
if(semid<0){
printf("semaphoreid get error!n");
return -1;
}
return semid;
}
int GenHH::creatsem(key_t semkey,int bigcount)
{
int semid,semval;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} semctl_arg;
semid=semget(semkey,2,IPC_CREAT|0600);
if(semid<0){
return -1;
}
if((semval=semctl(semid,1,GETVAL,0))<0)
printf("GETVAL error!n");
else if(semval==0){
semctl_arg.val=1;
if(semctl(semid,0,SETVAL,semctl_arg)<0)
printf("SETVAL errorn");
semctl_arg.val=bigcount;
if(( semctl(semid,1,SETVAL,semctl_arg)) < 0 )
printf("setval errorn");
}
return semid;
}
int GenHH::sem_open(int semid)
{
while(( semop(semid,&op_open,1) ) < 0 ){
if( errno==EINTR ) {
usleep(5000);
continue;
}
printf("sem op_open error!n");
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
GenHH *genHH1 = GenHH::getInstance();
GenHH *genHH2 = GenHH::getInstance();
cout<<genHH1->gen_hh()<<endl;
cout<<genHH2->gen_hh()<<endl;
return 0;
}