<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    Singleton

    #include <iostream>

    namespace ImplMethod1 {

    //Singleton template implementation: Method 1
    template <typename T>
    class Singleton {
    public:
        static T* GetInstance() {
            //You can consider thread-safe here if you will
            static T* s_ptr = new T;
            return s_ptr;
        }

    private:
        Singleton() {};
        virtual ~Singleton() {};
        Singleton(const Singleton&);
        Singleton& operator=(const Singleton&);
    };

    //How use it
    class MyClass {
    public:
        void func() {
            std::cout << __PRETTY_FUNCTION__ << std::endl;
        }

    private:
        friend class Singleton<MyClass>;
        MyClass() {};
        ~MyClass() {};
        MyClass(const MyClass&);
        MyClass& operator=(const MyClass&);
    };

    // end of namespace ImplMethod1

    //---------------------------------------------------------------------------

    namespace ImplMethod2 {

    //Singleton template implementation: Method 2
    template <typename T>
    class Singleton {
    public:
        static T* GetInstance() {
            //You can consider thread-safe here if you will
            static T* s_ptr = new T;
            return s_ptr;
        }

    protected:
        Singleton() {};
        virtual ~Singleton() {};

    private:
        Singleton(const Singleton&);
        Singleton& operator=(const Singleton&);
    };

    //How use it
    class MyClass: public Singleton<MyClass> {
    public:
        void func() {
            std::cout << __PRETTY_FUNCTION__ << std::endl;
        }

    private:
        friend class Singleton<MyClass>;
        MyClass() {};
        ~MyClass() {};
    };

    // end of namespace ImplMethod2

    //---------------------------------------------------------------------------

    namespace ImplMethod3 {

    //Singleton template implementation: Method 3
    class LazyInstantiation {
    protected:
        template <typename T>
        static T* CreateInstance(T* dummy) {
            //You can consider thread-safe here if you will
            return new T;
        }
        virtual ~LazyInstantiation() {};
    };

    template <typename T, typename InstantiationPolicy = LazyInstantiation>
    class Singleton : public InstantiationPolicy {
    public:
        static T* GetInstance() {
            //You can consider thread-safe here if you will
            static T* s_ptr = InstantiationPolicy::CreateInstance((T*)NULL);
            return s_ptr;
        }

    protected:
        Singleton() {};
        virtual ~Singleton() {};

    private:
        Singleton(const Singleton&);
        Singleton& operator=(const Singleton&);
    };

    //How use it
    class MyClass: public Singleton<MyClass> {
    public:
        void func() {
            std::cout << __PRETTY_FUNCTION__ << std::endl;
        }

    private:
        friend class LazyInstantiation;
        MyClass() {};
        ~MyClass() {};
    };

    // end of namespace ImplMethod3

    //---------------------------------------------------------------------------

    /**
     * 分析:
     * 方法1不需要繼承,但不能直觀的通過MyClass::GetInstance來獲取實例,且在MyClass里需要將拷貝構造以及operator=聲明為private;
     * 方法2通過繼承的方式,比方法1好;
     * 方法3在方法2的基礎上,進行了巧妙的擴展,可以讓用戶自定InstantiationPolicy,可以支持一些有特殊構造函數的類;
     *
     * 結論:
     * 綜合來看,方法2簡單易用,方法3實現成本稍高,但比方法2又精進了一步。
     
    */
    int main(int argc, char* argv[]) {
        ImplMethod1::MyClass* p1 = ImplMethod1::Singleton<ImplMethod1::MyClass>::GetInstance();
        p1->func();

        ImplMethod2::MyClass* p2 = ImplMethod2::MyClass::GetInstance();
        p2->func();

        ImplMethod3::MyClass* p3 = ImplMethod3::MyClass::GetInstance();
        p3->func();
        return 0;
    }

    posted on 2015-07-03 11:57 so true 閱讀(321) 評論(0)  編輯  收藏 所屬分類: C&C++

    主站蜘蛛池模板: 四只虎免费永久观看| 免费A级毛片无码免费视| 亚洲综合精品网站在线观看| 亚洲AV一区二区三区四区| 国产va精品免费观看| 亚洲综合无码无在线观看| 成年人视频在线观看免费| 亚洲精品成a人在线观看夫| 国产无遮挡又黄又爽免费视频| 国产精品亚洲а∨无码播放不卡| 日本一道综合久久aⅴ免费| 亚洲av中文无码乱人伦在线观看 | 免费看黄网站在线看 | 99在线热播精品免费99热| 婷婷久久久亚洲欧洲日产国码AV| 久久午夜无码免费| 亚洲免费在线视频| 日本阿v免费费视频完整版| 亚洲色大成WWW亚洲女子| 亚洲XX00视频| 国产永久免费高清在线| 亚洲视频手机在线| 成年午夜视频免费观看视频| 日本免费精品一区二区三区| 亚洲精品成人网站在线观看| 永久免费在线观看视频| 日本亚洲欧美色视频在线播放| 亚洲福利精品电影在线观看| 国产精品免费无遮挡无码永久视频| 亚洲日韩中文字幕天堂不卡| 国产日产成人免费视频在线观看| www免费黄色网| 亚洲国产精品成人综合久久久| 日本不卡免费新一二三区| 成人爽a毛片免费| 久久久久久亚洲精品影院| 亚洲国产成人久久综合野外| 182tv免费视视频线路一二三 | 亚洲精品无码99在线观看| 久久青草国产免费观看| 亚洲heyzo专区无码综合|