• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Mike's blog

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(17)

            我參與的團隊

            搜索

            •  

            最新評論

            所謂的單件類就是保證一個類僅有一個實例,并提供一個訪問它的全局訪問點。 

            Singleton可以看作是一種經過改進的全局變量,既在一個進程中只能有唯一的實例,不允許產生第二個這樣的對象。
            雖然單件類是最簡單的設計模式,但仍需小心使用,主要需注意:
            1.構造函數
            既然是只能有一個實例,那么構造函數自然不能被外部隨意調用,所以需要將其聲明為私有(private),包括默認構造、拷貝構造及賦值操作。至于是否需要實現要看具體應用。實例的產生需要一個輔助的成員函數(類似getInstance或creatInstance)。
            2.析構函數
            需要定義全局唯一的變量,我們首先會想到的就是靜態(static),沒錯,單件類也是通過靜態成員指針變量來實現單一。我們往往習慣于在析構函數中對成員指針進行內存釋放,但在單件類中是不可以這樣操作的,因為delete會調用類的析構,所以在自己的析構中delete自己的對象就會造成遞歸析構(無窮盡的析構現象)。
            UML類圖:

            實現代碼:
            1)Singleton.hpp
             1/********************************************************************
             2* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
             3********************************************************************/

             4/*
             5 * @file Singleton.hpp
             6 * @brief  Declare the class of Singleton.
             7 * @version 0.1
             8 * @since 0.1
             9 * @author chenwei<76487974@qq.com> 
            10 * @date 2010-7-19 Created it
            11 */

            12
            13#ifndef _SINGLETON_HPP
            14#define _SINGLETON_HPP
            15
            16#include <iostream>
            17
            18class Singleton
            19{
            20public:
            21    ~Singleton() {
            22        std::cout << "Singleton destructor." << std::endl;
            23    }

            24
            25    static Singleton* creatInstance();
            26    static void destroyInstance();
            27    void test() {
            28        std::cout << "Singleton test." << std::endl;
            29    }

            30
            31private:
            32    static Singleton* m_pInstance;
            33
            34    Singleton() {
            35        std::cout << "Singleton constructor." << std::endl;
            36    }

            37
            38    Singleton(Singleton&);
            39    Singleton& operator=(Singleton&);
            40}
            ;
            41
            42#endif
            43
            44

            2)Singleton.cpp
             1/********************************************************************
             2* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
             3********************************************************************/

             4/*
             5 * @file Singleton.cpp
             6 * @brief  Implement the methods of the class Singleton.
             7 * @version 0.1
             8 * @since 0.1
             9 * @author chenwei<76487974@qq.com> 
            10 * @date 2010-7-19    Created it
            11 */

            12
            13#include "Singleton.hpp"
            14#include <stdlib.h>
            15
            16Singleton* Singleton::m_pInstance = NULL;
            17
            18/**
            19 * @fn creatInstance 
            20 * @brief Create a Singleton instance.
            21 * @return A pointer to Singleton Instance, or NULL if failed. 
            22 * @author wei.chen (2010-7-19)
            23 */

            24Singleton* Singleton::creatInstance()
            25{
            26    std::cout << "Create the instance." << std::endl;
            27    if (!m_pInstance) {
            28        m_pInstance = new Singleton();
            29        if (!m_pInstance) {
            30            std::cout << "No memory to new for Singleton." << std::endl;
            31            abort();
            32        }

            33    }

            34
            35    return m_pInstance;
            36}

            37
            38/**
            39 * @fn destroyInstance 
            40 * @brief Release the memory for destroying the instance.
            41 * @author wei.chen (2010-7-19)
            42 */

            43void Singleton::destroyInstance()
            44{
            45    std::cout << "Destroy the instance." << std::endl;
            46    delete m_pInstance;
            47    m_pInstance = NULL;
            48}

            49

            3)Main.cpp
             1/********************************************************************
             2* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
             3********************************************************************/

             4/*
             5 * @file Main.cpp
             6 * @brief The entrance of the program.
             7 * @version 0.1
             8 * @since 0.1
             9 * @author chenwei<76487974@qq.com> 
            10 * @date 2010-7-19    Created it
            11 */

            12
            13#include "Singleton.hpp"
            14
            15/**
            16 * @fn main 
            17 * @brief The entrance of the program.
            18 * @return int 
            19 * @retval 0-normal 
            20 * @author wei.chen (2010-7-19)
            21 */

            22int main()
            23{
            24    Singleton* singletonTest = Singleton::creatInstance();
            25    if (!singletonTest) {
            26        std::cout << "Create Instance failed." << std::endl;
            27        return -1;
            28    }

            29
            30    singletonTest->test();
            31    Singleton::destroyInstance();
            32
            33    return 0;
            34}

            35

            posted on 2010-07-19 23:39 老狼 閱讀(1689) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            99久久精品国产麻豆| 国产精品久久久久9999| 久久免费国产精品一区二区| 久久久久99精品成人片欧美| 久久电影网2021| 色婷婷久久综合中文久久蜜桃av| 奇米影视7777久久精品| 九九99精品久久久久久| 青青草原综合久久大伊人| 久久久久久久97| 国内精品久久久久久中文字幕 | 久久久青草久久久青草| 97精品国产97久久久久久免费| 国产成年无码久久久免费| 久久九九青青国产精品| 2020国产成人久久精品| 99热精品久久只有精品| 99精品久久久久久久婷婷| 久久久久波多野结衣高潮| 理论片午午伦夜理片久久| 91精品国产91热久久久久福利| 久久综合九色综合网站| 久久伊人亚洲AV无码网站| 久久香蕉国产线看观看99| 久久久久亚洲Av无码专| 亚洲精品白浆高清久久久久久| 思思久久99热免费精品6| 国产精品欧美久久久久天天影视| 亚洲va久久久噜噜噜久久男同| 婷婷久久综合九色综合九七| segui久久国产精品| 91久久精品视频| 久久亚洲国产精品一区二区| 丁香狠狠色婷婷久久综合| 久久久久免费看成人影片| 久久精品毛片免费观看| 无码日韩人妻精品久久蜜桃| 亚洲色婷婷综合久久| 日韩久久久久久中文人妻| 精品乱码久久久久久久| 国产99久久久国产精品~~牛|