• <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++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(17)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            所謂的單件類就是保證一個(gè)類僅有一個(gè)實(shí)例,并提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。 

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

            實(shí)現(xiàn)代碼:
            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 老狼 閱讀(1688) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C/C++
            色综合久久88色综合天天 | 亚洲综合久久综合激情久久| 久久久久亚洲精品日久生情| 久久国产劲爆AV内射—百度| 欧洲成人午夜精品无码区久久 | 日韩影院久久| 久久精品国产亚洲AV不卡| 精品久久久久久久久中文字幕| 久久e热在这里只有国产中文精品99 | 久久久久久久综合综合狠狠| 7777精品伊人久久久大香线蕉| 国产精品久久99| 久久久久亚洲国产| 超级碰久久免费公开视频| 国内精品久久久久久久久电影网| 久久久久中文字幕| 久久精品中文字幕无码绿巨人| 国产99久久九九精品无码| 亚洲中文字幕无码久久精品1| 日批日出水久久亚洲精品tv| 欧美激情精品久久久久| 久久精品人成免费| 精品国产青草久久久久福利| 色8激情欧美成人久久综合电| 久久综合欧美成人| 91精品国产综合久久精品| 18岁日韩内射颜射午夜久久成人| 久久久精品国产Sm最大网站| 国产无套内射久久久国产| 97久久天天综合色天天综合色hd | 欧美综合天天夜夜久久| 久久精品国产亚洲AV无码偷窥| 蜜桃麻豆WWW久久囤产精品| 久久久WWW成人免费毛片| 久久久精品视频免费观看| 国产精品成人99久久久久| 色综合久久综精品| 久久精品无码一区二区三区| 久久97精品久久久久久久不卡| 国产∨亚洲V天堂无码久久久| 久久电影网2021|