Design Pattern-singleton (Singleton pattern)

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.