SINGLETON mode C ++ implementation research (sample code)

zhaozj2021-02-08  351

[Annex 1: Presentation Code List]

/ * //

Author: Zhang Youbang

Time: October 9, 2002

Description: Implement Singleton

/ * //

#include

#include

// First implementation (using template functions)

Class mysingleton1

{

Private:

Mysingleton1 () {cout << _t ("construct mysingleton1") << endl;}

MYSINGLETON1 & OPERATOR = (const mysingleton1&) {}

Template

Friend T & GetInstanceRef ();

PUBLIC:

~ Mysingleton1 () {cout << _t ("destroy mySingleton1") << endl;}

PUBLIC:

Void dosomething () {cout << _t ("do something here in mysingleton1) << endl;}

}

Template

T & GetInstanceRef ()

{

Static T_INSTANCE;

Return_INSTANCE;

}

Template

T * GetInstancePtr ()

{

Return & GetInstanceRef ();

}

// Second implementation (using template class)

Template

Class Singletonwraper

{

PUBLIC:

Static T & GetInstanceRef ()

{

Static T_INSTANCE;

Return_INSTANCE;

}

Static Const T & GetInstanceConst ()

{

Return getInstanceRef ();

}

Static T * getInstancePtr ()

{

Return & GetInstanceRef ();

}

}

#define define_singleton (classname); /

PUBLIC: /

Friend Class Singletonwraper ; /

Typedef class singleletonwraper Singletonwraper; /

Typedef Singletonwraper Singletoninterface; /

PRIVATE: /

Const classname & operator = (const classname) /

{/

Return Singletoninterface :: getInstanceRef (); /

} /

ClassName (const classname); /

PRIVATE: /

Static Void Operator Delete (Void * P, SIZE_T N) /

{/

Throw -1; /

} // end of define declare_singleton (classname);

Class mysingleton2

{

Define_nsingleton (Mysingleton2);

Private:

Mysingleton2 () {cout << _t ("construct mySingleton2") << endl;} public:

~ Mysingleton2 () {cout << _t ("destroy mySingleton2) << endl;}

PUBLIC:

Void Dosomething () {cout << _t ("do something here in mysingleton2) <<" << endl;}

}

// The third implementation (implemented by the class itself, automatically destroying the object, compared to it, it is simpler)

#define declare_singleton (classname); /

PUBLIC: /

Static ClassName & GetInstanceRef () /

{/

Static classname _INSTANCE; /

Return_INSTANCE; /

} /

Static const classname & getInstanceconst () /

{/

Return getInstanceRef (); /

} /

Static classname * getInstancePtr () /

{/

Return & getInstanceRef (); /

} /

Const classname & operator = (const classname) /

{/

Return getInstanceRef (); /

} /

PRIVATE: /

ClassName (const classname); /

Static Void Operator Delete (Void * P, SIZE_T N) /

{/

Throw -1; /

} // end of define declare_singleton (classname);

Class mysingleton3

{

Declare_singleton (Mysingleton3);

Private:

Mysingleton3 () {cout << _t ("construct mySingleton3) << endl; id = 0;}

PUBLIC:

Int ID;

~ Mysingleton3 () {cout << _t ("destroy mySingleton3) << endl;}

Void Dosomething () {cout << _t ("do something here in mysingleton3, id =") << id << endl;}

}

// The fourth implementation ("Design Patterns", made some modifications)

// (due to the class itself, manual and automatic destruction)

#define allow_singleton (classname); /

PRIVATE: /

Static classname * _INSTANCE; /

/

PUBLIC: /

Static ClassName & GetInstanceRef () /

{/

IF (_instance == 0) /

_INSTANCE = New classname; /

Return * _INSTANCE; /

} /

Static classname * getInstancePtr () /

{/

Return & getInstanceRef (); /

} /

Static ReleaseInstance () / {/

IF (_instance! = 0) /

{/

Delete _INSTANCE; /

_INSTANCE = 0; /

} /

} // end of allow_singleton (classname);

#define uste_nsingleton (classname); /

Classname * classname :: _ instance = 0; /

Static Class DestructHelper _ ## ClassName /

{/

PUBLIC: /

~ DestRucthelper _ ## classname () {classname :: releaseinstance ();} /

} DESTRUCTHELPERINSTANCE _ ## ClassName

// end of uspement_single (classname);

Class mysingleton4

{

Private:

Mysingleton4 () {cout << _t ("construct mySingleton4") << endl;} // constructor private

~ Mysingleton4 () {cout << _t ("destroy mySingleton4) << Endl;} // destructor where you can put

Allow_singleton (MySingleton4);

PUBLIC:

Void dosomething () {cout << _t ("do something here in mysingleton4) << endl;}

}

Implement_singleton (Mysingleton4);

//test

Void_tmain (int Argc, char * argv [])

{

// Test the first implementation

COUT << _T ("*************** TEST OF Test Implementation ***************") << Endl;

MYSINGLETON1 * myObj1;

MYOBJ1 = GetInstancePtr ();

MYOBJ1-> DOSMETHING ();

GetInstanceRef (). DOSMETHING ();

// Test the second implementation

Cout << Endl << _t ("*************** TEST OF THE Second Implementation **************") << Endl;

Mysingleton2 * myobj2;

MYOBJ2 = Singletonwraper :: getInstancePtr ();

MYOBJ2-> DOSMETHING ();

// mysingleton2 myobj22 (* myobj2); // error

Mysingleton2 :: Singletonterface :: getInstanceref (). Dosomething ();

// Test the third implementation

COUT << Endl << _t ("*****************************") << endl;

MYSINGLETON3 * MyObj3 = mysingleton3 :: getInstancePtr ();

MyObj3-> ID = 1;

MYOBJ3-> DOSMETHING (); mysingleton3 & myobj33 = mysingleton3 :: getinstanceref ();

myobj33 = * myobj3;

Try

{

Delete myobj3;

}

Catch (...)

{

Cout <<_T ("Your Object Cannot Be deleded.") << Endl;

}

Myobj33.id = 2;

Myobj33.dosomething ();

MYOBJ3-> DOSMETHING ();

// Test the fourth implementation

COUT << Endl << _t ("************** TEST OF THE FOURTH IMPLEMATION China *************") << Endl;

MySingleton4 * myobj4 = mysingleton4 :: getInstanceptr ();

MYOBJ4-> DOSMETHING ();

Mysingleton4 :: getInstanceref (). DOSMETHING ();

Cout << _T ("********************* * End of all testing ***************** *** ") << Endl << endl;

Cout <<_T ("FOLLOWING IS The Automatic Garbage Collection Process:") << Endl << endl;

}

[Annex II: Demo Procedure Run]

************** TEST OF TEST OF TEST OF TEST OF TEST IMPLEMENTION ****************

Construct mysingleton1

Do Something Here in Mysingleton1

Do Something Here in Mysingleton1

************** TEST OF TEST OF TEST OF TEST OF THE SECOND IMPLEMENTION ***************

Construct mysingleton2

Do Something Here in Mysingleton2

Do Something Here in Mysingleton2

************** TEST OF TEST OF THIRD IMPLEMENTATION ****************

Construct mysingleton3

Do Something Here in Mysingleton3, ID = 1

Destroy MySingleton3

Your Object Cannot Be deleded.

Do Something Here in Mysingleton3, ID = 2

Do Something Here in Mysingleton3, ID = 2

************** TEST OF TEST OF TEST OF Test Of The Fourth Implement China **************

Construct mysingleton4

Do Something Here in Mysingleton4

Do Something Here in Mysingleton4

*************************************************************************************

FOLLOWING IS The Automatic Garbage Collection Process:

Destroy MySingleton3

Destroy mysingleton2

Destroy mysingleton1destroy mysingleton4

转载请注明原文地址:https://www.9cbs.com/read-645.html

New Post(0)