Singleton模式的C 實現研究

2021-04-27 08:44:25 字數 4511 閱讀 8585

[附件一:演示程式**清單]

/*//

描述:實現singleton

/*//

#include

#include

//第一種實現(使用模板函式)

class

mysingleton1

mysingleton1 & operator =(const mysingleton1&){}

template

friend t& getinstanceref();

public:

~mysingleton1()

public:

void dosomething() };

template

t& getinstanceref()

template

t* getinstanceptr()

//第二種實現(使用模板類)

template

class

singletonwraper

static

const t& getinstanceconst()

static t* getinstanceptr()

}; #define

define_singleton(classname); /

public

: /

friend

class singletonwraper; /

typedef

class singletonwrapersingletonwraper; /

typedef singletonwraper singletoninte***ce; /

private

: /

const classname& operator=(const classname&) / /

classname(const classname&); /

private

: /

static

void

operator

delete(void *p, size_t n) /

//end of define declare_singleton(classname);

class

mysingleton2

public:

~mysingleton2()

public:

void dosomething() };

//第三種實現(由類自身實現,自動銷毀物件,相比之下,它更簡單)

#define

declare_singleton(classname); /

public

: /

static classname& getinstanceref() / /

static

const classname& getinstanceconst() / /

static classname* getinstanceptr() / /

const classname& operator=(const classname&) / /

private

: /

classname(const classname&); /

static

void

operator

delete(void *p, size_t n) /

//end of define declare_singleton(classname);

class

mysingleton3

public:

int id;

~mysingleton3()

void dosomething() };

//第四種實現(《design patterns》裡的,做了一些修改)

//(由類自身實現,手動與自動銷毀物件)

#define

allow_singleton(classname); /

private

: /

static classname* _instance; / /

public

: /

static classname& getinstanceref() / /

static classname* getinstanceptr() / /

static releaseinstance() / /

} //end of allow_singleton(classname);

#define

implement_singleton(classname); /

classname* classname::_instance = 0; /

static

class destructhelper_##classname / /

} destructhelperinstance_##classname;

//end of implement_single(classname);

class

mysingleton4

//建構函式私有

~mysingleton4()//析構函式放**都可以

allow_singleton(mysingleton4);

public:

void dosomething() };

implement_singleton(mysingleton4);

//測試

void

_tmain(int argc, char *argv)

catch(...)

myobj33.id = 2;

myobj33.dosomething();

myobj3->dosomething();

//測試第四種實現

cout << endl << _t("**************test of the fourth implementation**************") << endl;

mysingleton4 *myobj4 = mysingleton4::getinstanceptr();

myobj4->dosomething();

mysingleton4::getinstanceref().dosomething();

cout << _t("**********************end of all testing*********************") << endl << endl;

cout << _t("following is the automatic garbage collection process:") << endl << endl; }

[附件二:演示程式執行結果]

**************test of the first implementation***************

construct mysingleton1

do something here in mysingleton1

do something here in mysingleton1

**************test of the second implementation**************

construct mysingleton2

do something here in mysingleton2

do something here in mysingleton2

**************test of the third implementation***************

construct mysingleton3

do something here in mysingleton3, id = 1

destroy mysingleton3

your object cannot be deleted.

do something here in mysingleton3, id = 2

do something here in mysingleton3, id = 2

**************test of the fourth implementation**************

construct mysingleton4

do something here in mysingleton4

do something here in mysingleton4

**********************end of all testing*********************

following is the automatic garbage collection process:

destroy mysingleton3

destroy mysingleton2

destroy mysingleton1

destroy mysingleton4

C 實現Singleton模式

單例模式定義 保證乙個類僅有乙個例項,並提供乙個該例項的全域性訪問點。類的宣告 class singleton singleton singleton m instance nullptr 解法1 執行緒非安全版本 singleton singleton getinstance return m i...

Singleton設計模式的C 實現

singleton模式 singleton 譯為單件或單態 模式是設計模式中比較簡單而常用的模式。有些時候在整個應用程式中,會要求某個類有且只有乙個例項,這個時候可以採用singleton模式進行設計。用singleton模式設計的類不僅能保證在應用中只有乙個例項,而且提供了一種非全域性變數的方法進...

Singleton模式的實現

設計乙個類,我們只能生成該類的乙個例項。因為我們只能生成乙個例項,所以我們必須把建構函式設定為私有函式已禁止他人建立例項。可以定義乙個靜態的例項,在需要的時候建立該例項。public sealed class sinleton1 private static singleton1 instance ...