Thinking about the design pattern of C + + single case class

Source: Internet
Author: User
Tags class definition


This blog post assumes that there is a manager management class that explores the design patterns of singleton classes.

General Practice

class declaration:

classmanager{ Public:~Manager ();//provides single-instance object accessStaticmanager*getinstance ();//Delete a singleton objectvoiddeleteinstance ();voiddosometing ();Private://constructors declared as protection methodsManager ();//Singleton object PointerStaticmanager*S_manager;};

class method Definition:

//Singleton object pointer is initialized to nullptr, preventing pointing to undefined datamanager* Manager::s_manager =nullptr;//provides singleton class object accessStaticmanager*manager::getinstance () {if(!s_manager) S_manager =NewCachemanger ();returnS_manager;} //Delete a singleton classvoidManager::d eleteinstance () {if(S_manager) {deleted S_manager;s_manager= nullptr;}//don't forget to assign a null pointer, otherwise point to undefined data} voidManager::d osometing () {//dosometing}

So we can use it in our usual program.

manager::getinstance ()->dosomething ();

To use a singleton class to do certain things.

Thinking

In the actual project, I found that there is a lot of factory management, such as the need for a singleton class, if each class to write getinstance (), Deleteinstance () method is too cumbersome.

Based on this disadvantage (lazy point), naturally think of using templates.
(Since most singleton classes are common getinstance (), Deletedinstance (), and templates can overload these common class functions, the idea is as follows)

Template Resolution Method

Template:

template<classT>classsingleton{Private:StaticS Ms_instance; Public:StaticS Mgetinstance ();Static voiddeleteinstance ();}; //The template class provides a singleton class of Accesstemplate<classT>T* singleton<t>:: getinstance () {if(!s_instance) S_instance =NewT ();returns_instance;} //template class Delete Singleton classtemplate<classT>voidSingleton<t>::d eleteinstance () {if(s_instance) {Deletes_instance;s_instance=nullptr;}} //static variable initialization of template classtemplate<classT>T* Singleton<t>::s_instance = nullptr;

class declaration:

class  Public Singleton<manager>{public:~void//  Because singleton<cachemanger> cannot access the private method of Cachemanger (that is, that constructor), you can only use friends to provide permissions. Friend Singleton<cachemanger>private:// constructor declared as private method Manager ();};

Class definition:

void Manager::d osomething () {//dosometing}

How to use:

Cachemanger::getinstance ()->dosomething ();
Summarize

Compared to the first general practice,
The manager transfers the Singleton method to the singleton implementation, thus
This makes the manager's class declarations and definitions a lot more refreshing and concise.
This enables programmers to avoid redundant code and improve code efficiency (laziness) in projects that require multiple singleton classes.

(Warning: This article does not consider thread safety)

Combined, according to the individual humble opinion, in a project requires a large number of singleton classes, the second is a single template method is more appropriate.
And usually use the first kind of single-class method is not bad.

Thinking about the design pattern of C + + single case class

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.