C++單例模式應(yīng)用實(shí)例
本文實(shí)例講述了C++單例模式及其相關(guān)應(yīng)用方法,分享給大家供大家參考。具體方法分析如下:
定義:
一個(gè)類有且僅有一個(gè)實(shí)例,并且提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。
要點(diǎn):
1、類只能有一個(gè)實(shí)例;
2、必須自行創(chuàng)建此實(shí)例;
3、必須自行向整個(gè)系統(tǒng)提供此實(shí)例。
實(shí)現(xiàn)一:?jiǎn)卫J浇Y(jié)構(gòu)代碼
singleton.h文件代碼如下:
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
class Singleton
{
public:
static Singleton* GetInstance();
protected:
Singleton();
private:
static Singleton *_instance;
};
#endif
singleton.cpp文件代碼如下:
#include "singleton.h"
#include <iostream>
using namespace std;
Singleton* Singleton::_instance = 0;
Singleton::Singleton()
{
cout<<"create Singleton ..."<<endl;
}
Singleton* Singleton::GetInstance()
{
if(0 == _instance)
{
_instance = new Singleton();
}
else
{
cout<<"already exist"<<endl;
}
return _instance;
}
main.cpp文件代碼如下:
#include "singleton.h"
int main()
{
Singleton *t = Singleton::GetInstance();
t->GetInstance();
return 0;
}
實(shí)現(xiàn)二:打印機(jī)實(shí)例
singleton.h文件代碼如下:
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
class Singleton
{
public:
static Singleton* GetInstance();
void printSomething(const char* str2Print);
protected:
Singleton();
private:
static Singleton *_instance;
int count;
};
#endif
singleton.cpp文件代碼如下:
#include "singleton.h"
#include <iostream>
using namespace std;
Singleton* Singleton::_instance = 0;
Singleton::Singleton()
{
cout<<"create Singleton ..."<<endl;
count=0;
}
Singleton* Singleton::GetInstance()
{
if(0 == _instance)
{
_instance = new Singleton();
}
else
{
cout<<"Instance already exist"<<endl;
}
return _instance;
}
void Singleton::printSomething(const char* str2Print)
{
cout<<"printer is now working , the sequence : "<<++count<<endl;
cout<<str2Print<<endl;
cout<<"done\n"<<endl;
}
main.cpp文件代碼如下:
#include "singleton.h"
int main()
{
Singleton *t1 = Singleton::GetInstance();
t1->GetInstance();
t1->printSomething("t1");
Singleton *t2 = Singleton::GetInstance();
t2->printSomething("t2");
return 0;
}
Makefile文件:
CC=g++
CFLAGS = -g -O2 -Wall
all:
make singleton
singleton:singleton.o\
main.o
${CC} -o singleton main.o singleton.o
clean:
rm -rf singleton
rm -f *.o
.cpp.o:
$(CC) $(CFLAGS) -c -o $*.o $<
運(yùn)行效果如下圖所示:
可以看到,對(duì)打印順序count的計(jì)數(shù)是連續(xù)的,系統(tǒng)中只有一個(gè)打印設(shè)備。
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
相關(guān)文章
C++實(shí)現(xiàn)AVL樹(shù)的基本操作指南
AVL樹(shù)是高度平衡的而二叉樹(shù),它的特點(diǎn)是AVL樹(shù)中任何節(jié)點(diǎn)的兩個(gè)子樹(shù)的高度最大差別為1,下面這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)AVL樹(shù)的相關(guān)資料,需要的朋友可以參考下2022-01-01
C語(yǔ)言銀行系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言銀行系統(tǒng)課程設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
用C實(shí)現(xiàn)PHP擴(kuò)展 Image_Tool 圖片常用處理工具類的使用
該擴(kuò)展是基于ImageMagick基礎(chǔ)實(shí)現(xiàn)的,圖片操作調(diào)用的是ImageMagick API2013-04-04
Inline Hook(ring3)的簡(jiǎn)單C++實(shí)現(xiàn)方法
這篇文章主要介紹了Inline Hook(ring3)的簡(jiǎn)單C++實(shí)現(xiàn)方法,需要的朋友可以參考下2014-08-08
淺理解C++ 人臉識(shí)別系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了淺理解C++ 人臉識(shí)別系統(tǒng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C++不使用變量求字符串長(zhǎng)度strlen函數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++不使用變量求字符串長(zhǎng)度strlen函數(shù)的實(shí)現(xiàn)方法,實(shí)例分析了strlen函數(shù)的實(shí)現(xiàn)原理與不使用變量求字符串長(zhǎng)度的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06
QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)圖文詳解
QT開(kāi)發(fā)這幾年大大小小項(xiàng)目做了不少,花了點(diǎn)時(shí)間對(duì)知識(shí)點(diǎn)總結(jié)整合了一部分,下面這篇文章主要給大家介紹了關(guān)于QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-11-11
如何實(shí)現(xiàn)socket網(wǎng)絡(luò)編程的多線程
首先,學(xué)好計(jì)算機(jī)網(wǎng)絡(luò)知識(shí)真的很重要。雖然,學(xué)不好不會(huì)影響理解下面這個(gè)關(guān)于宏觀講解,但是,學(xué)好了可以自己打漁吃,學(xué)不好就只能知道眼前有魚吃卻打不到漁。在Java中網(wǎng)絡(luò)程序有2種協(xié)議:TCP和UDP,下面可以和小編一起學(xué)習(xí)下2019-05-05

