Purpose: ensure that a class has only one instance and provides a global access point to it.
Resolution:
The Singleton mode is actually a replacement policy for Global static variables.
The two functions of the mode are implemented through the following mechanism in C ++: 1) only one instance provides one
Static member variables of the class. We all know that static member variables of the class are unique to all objects of the class.
(1) provide a global access point to access it, that is, provide the corresponding access to this static member
The static member function is unique for all objects of the class. You can directly use the class in C ++.
Without initializing a class object.
The following implementation is actually a simple implementation of Singleton, which is not particularly common.
If many Singleton modes need to be used in a project, a singl
Eton template class. The template parameters of the template class are classes in the singleton mode. For example
Now:
Template <typename T>
Class Singleton
{
// Class declaration
};
// The class that requires the singleton Mode
Class Test
: Public Singleton <Test>
{
// Class declaration
};
However, the following implementation is the simplest implementation method, which plays a role in demonstration.
Implementation:
1) singleton. h
/*************************************** ***********************
******
Created: 2006/07/20
Filename: Singleton. h
Author: Li Chuang
Http://www.cppblog.com/converse/
Purpose: Singleton mode demonstration Code
**************************************** ************************
*****/
# Ifndef singleton_h
# Define singleton_h
Class Singleton
{
Public:
Singleton (){};
~ Singleton (){};
// Static member functions that provide global access interfaces
Static Singleton * getinstanceptr ();
Static Singleton getinstance ();
Void test ();
PRIVATE:
// Static member variable, providing a globally unique instance
Static Singleton * m_pstatic;
};
# Endif
2) singleton. cpp
/*************************************** ***********************
******
Created: 2006/07/20
Filename: Singleton. cpp
Author: Li Chuang
Http://www.cppblog.com/converse/
Purpose: Demo code of Singleton Mode
**************************************** ************************
*****/
# Include "Singleton. H"
# Include <iostream>
// Static member variables of the class should be defined in Vitro
Singleton * singleton: m_pstatic = NULL;
Singleton * singleton: getinstanceptr ()
{
If (null = m_pstatic)
{
M_pstatic = new Singleton ();
}
Return m_pstatic;
}
Singleton singleton: getinstance ()
{
Return * getinstanceptr ();
}
Void singleton: Test ()
{
STD: cout <"test! \ N ";
}
3) Main. cpp
/*************************************** ***********************
******
Created: 2006/07/20
Filename: Main. cpp
Author: Li Chuang
Http://www.cppblog.com/converse/
Purpose: Test code for Singleton Mode
**************************************** ************************
*****/
# Include "Singleton. H"
# Include <stdlib. h>
Int main ()
{
// You do not need to initialize class objects.
Singleton: getinstanceptr ()-> test ();
Singleton: getinstance (). Test ();
System ("pause ");
Return 0;
}