Design Mode C ++ study Note 3 (Singleton Mode)

Source: Internet
Author: User

The Singleton mode seems simple, that is, only one instance of this class can be generated in the system. Since it is so simple, code is directly pasted. For more details, refer to the original author's blog: cbf4life.cnblogs.com.

. Explanation

Main (), Minister

CEmperor, a class that requires a singleton

Note: many ministers worship only one emperor. Embodied in the object-oriented aspect, CEmperor defines a static pointer and a static function, private constructor, destructor, constructor copy, and overload value assignment statement.

Note: thread security is implemented using mutex.

Check the Code:

// Emperor. h

#pragma once#include <iostream>using std::cout;using std::endl;using std::string;class CEmperor{public:    static CEmperor * GetInstance();    static void ReleaseInstance();    void EmperorInfo(void);    void SetEmperorTag(string tag);private:    CEmperor(void);    virtual ~CEmperor(void);    CEmperor(const CEmperor&);    CEmperor& operator=(const CEmperor&);    static CEmperor *m_pEmperor;    static HANDLE m_pMutex;    string m_EmperorTag;    class CGarbo    {    public:        CGarbo()        {            cout << "Create Garbo" << endl;        }        ~CGarbo()        {            cout << "Destroy Garbo" << endl;            if (NULL != m_pEmperor)            {                WaitForSingleObject(m_pMutex, INFINITE);                if (NULL != m_pEmperor)                {                    cout << "Remove instance" << endl;                    delete m_pEmperor;                    m_pEmperor = NULL;                }                ReleaseMutex(m_pMutex);            }            if (NULL != m_pMutex)            {                cout << "Delete mutex" << endl;                CloseHandle(m_pMutex);                m_pMutex = NULL;            }        }    };    static CGarbo m_Garbo;};//Emperor.cpp
# Include "StdAfx. h "# include" Emperor. h "# include <iostream> using std: cout; using std: endl; using std: string; CEmperor * CEmperor: m_pEmperor = NULL; HANDLE CEmperor :: m_pMutex = CreateMutex (NULL, FALSE, NULL); CEmperor: CGarbo CEmperor: m_Garbo; CEmperor: CEmperor (void) {cout <"Create CEmperor Instance" <endl;} CEmperor ::~ CEmperor (void) {cout <"Destroy CEmperor Instance and release its resource" <endl;} void CEmperor: EmperorInfo (void) {char msgBuffer [50] = {0}; sprintf_s (msgBuffer, 50, "Emperor? A 3, a 3, a 3 ...... (% s ). ", m_EmperorTag.c_str (); string msg (msgBuffer); cout <msg. c_str () <endl;} CEmperor * CEmperor: GetInstance () {if (NULL = m_pEmperor) {WaitForSingleObject (m_pMutex, INFINITE); if (NULL = m_pEmperor) m_pEmperor = new CEmperor (); ReleaseMutex (m_pMutex);} return m_pEmperor;} void CEmperor: ReleaseInstance () {if (NULL! = M_pEmperor) {WaitForSingleObject (m_pMutex, INFINITE); if (NULL! = M_pEmperor) {delete m_pEmperor; m_pEmperor = NULL;} ReleaseMutex (m_pMutex) ;}} void CEmperor: SetEmperorTag (string tag) {m_EmperorTag = tag ;}// Singleton. cpp
# Include "stdafx. h" # include "Emperor. h"
void DoIt()

{

    CEmperor *pEmperor1 = CEmperor::GetInstance();    pEmperor1->SetEmperorTag("95");    pEmperor1->EmperorInfo();    CEmperor *pEmperor2 = CEmperor::GetInstance();    pEmperor2->EmperorInfo();    CEmperor *pEmperor3 = CEmperor::GetInstance();    pEmperor3->EmperorInfo();    CEmperor *pEmperor4 = pEmperor3;    pEmperor4->EmperorInfo();    CEmperor *pEmperor5 = NULL;    pEmperor5 = pEmperor4;    pEmperor5->EmperorInfo();    CEmperor::ReleaseInstance();

}

int _tmain(int argc, _TCHAR* argv[])

{

    DoIt();

    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);    _CrtDumpMemoryLeaks();    return 0;}

The Singleton mode is relatively simple, but when using it in a project, you must explicitly call the CEmperor GetInstance function to obtain the instance. In C #
A simpler method is to declare a read-only static variable, which is much easier than C ++.
But C ++ is more attractive for people to study. This is the pleasure of software development.

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.