• <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 老狼 閱讀(1705) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            精品免费久久久久久久| 国产精品无码久久久久久| 久久香蕉超碰97国产精品| 99久久精品免费看国产一区二区三区 | 色综合久久中文字幕综合网| 久久精品国产99国产精品澳门| 无码人妻久久一区二区三区| 97精品伊人久久久大香线蕉| 99精品国产99久久久久久97| 亚洲国产精品无码久久一区二区| 久久午夜无码鲁丝片秋霞| 久久热这里只有精品在线观看| 午夜久久久久久禁播电影| 久久久久久亚洲AV无码专区| 2022年国产精品久久久久| 91精品免费久久久久久久久| 久久久久亚洲爆乳少妇无| 久久久久久国产精品无码下载 | 精品国产青草久久久久福利| 久久99精品久久久久久秒播 | 久久伊人色| 亚洲国产欧美国产综合久久| 精品久久久久久久久中文字幕| 久久精品夜色噜噜亚洲A∨| 久久人人爽人人爽人人爽| 97久久久久人妻精品专区| 久久996热精品xxxx| 亚洲AV无码久久精品蜜桃| 国产精品久久久久久影院| 色综合久久天天综线观看| 久久精品人人做人人爽97| 久久99久久无码毛片一区二区| 久久精品国产2020| 国产精品九九久久免费视频 | 狠狠久久亚洲欧美专区| 亚洲国产成人久久笫一页| 精品国产乱码久久久久久1区2区 | 国产精品久久久久aaaa| 色诱久久av| 青青青国产成人久久111网站| 欧美精品福利视频一区二区三区久久久精品|