The singleton of C + + design pattern

Source: Internet
Author: User
First, the function guarantees that a class has only one instance. Third, advantages and disadvantages Singleton mode is a substitute for the "global variable" appears.   So it has the characteristics of global variables: globally visible, through the entire life of the application, it also has a global variable does not have the nature: an object instance of the same type can only have one. Iv. the realization of the singleton definition in the textbook is as follows:
Class Singleton
{
Public
Static singleton* Instance ();
PRotected:
Singleton () {}
Private
Static Singleton *_instance;
Singleton (const singleton&);
singleton& operator= (const singleton&);
} ; singleton* singleton::_instance = NULL; singleton* singleton::instance ()
{
(_instance = = NULL)? _instance = new Singleton (): 0; Lazy initialization
return _instance;
} (1) because the pointer is returned, to prevent the user from calling the delete function, the static Singleton *_instance can be changed, and the static Singleton _instance is defined in instance () instead. This is obviously more secure, and also has the feature of lazy initialization (that is, it was created the first time it was accessed).

(2) Suppose you need to derive subclasses from Singleton, and subclasses need to be of the same nature, to create only one instance. I think it's difficult. The root cause is that the instance () function is not a virtual function and has no polymorphic properties. A common method is to move the instance () function into a subclass, at which point the static Singleton *_instance can be used instead of static Singleton _instance, unless the _instance is moved to the subclass, No matter what you do, it's not elegant. Another way is to use a template. The concrete use of what method, only according to the actual situation weigh.

V. Example code (1) No subclass of case namespace Designpattern_singleton
{Class Singleton
{
Public
Static singleton* Instance () {static Singleton _instance; return &_instance;}
Protected
Singleton () {}
Private
Singleton (const singleton&);
singleton& operator= (const singleton&);
} ;
} Client code:
{
using namespace Designpattern_singleton;
Singleton *p = Singleton::instance ();
......
} (2) case with subclass
Method One:
Namespace Designpattern_singleton
{
Class Singleton
Class Singleton
{
Protected
Singleton () {}
Static Singleton *_instance;
Private
Singleton (const singleton&);
singleton& operator= (const singleton&);
} ;
singleton* singleton::_instance = NULL; Class Concretesingleton
Class Concretesingleton:public Singleton
{
Public
Static singleton* Instance ();
Protected
Concretesingleton () {}
} ; singleton* concretesingleton::instance ()
{
(_instance = = NULL)? _instance = new Concretesingleton (): 0;
return _instance;
}
} Client code:
{
using namespace Designpattern_singleton;
Singleton *p = Concretesingleton::instance ();
} Method Two:
Namespace Designpattern_singleton
{
Class Singleton
Class Singleton
{
Protected
Singleton () {}
Private
Singleton (const singleton&);
singleton& operator= (const singleton&);
} ; Class Concretesingleton
Class Concretesingleton:public Singleton
{
Public
Static singleton* Instance () {static Concretesingleton _instance; return &_instance;}
Protected
Concretesingleton () {}
} ;
} Client code:
{
using namespace Designpattern_singleton;
Singleton *p = Concretesingleton::instance ();

} Method Three:
Namespace Designpattern_singleton
{
Template < class T >
Class Singleton
{
Public
Static t* Instance () {static T _instance; return &_instance;}
Protected
Singleton () {}
Private
Singleton (const Singleton &);
singleton& operator= (const singleton&);
} ; Class Concretesingleton:public singleton< Concretesingleton > {};
} Client code
{
using namespace Designpattern_singleton; Concretesingleton *p = Concretesingleton::instance ();
}

The above is the C + + design mode of singleton content, more relevant articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.