<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++

    主站蜘蛛池模板: 亚洲一区欧洲一区| 91亚洲精品视频| 亚洲欧美成人一区二区三区| 日本免费xxxx色视频| 亚洲嫩模在线观看| 亚洲精品免费观看| 亚洲国产成人精品不卡青青草原| 91免费国产视频| 亚洲精品国精品久久99热一| 黄色网站软件app在线观看免费| 亚洲自偷自偷图片| 日韩午夜理论免费TV影院| 亚洲va在线va天堂va不卡下载| 99ee6热久久免费精品6| 亚洲综合男人的天堂色婷婷| 五月婷婷在线免费观看| 久久亚洲最大成人网4438| 免费无码又爽又刺激毛片| 无遮挡呻吟娇喘视频免费播放| 亚洲偷自拍拍综合网| 国产精品免费观看调教网| 亚洲第一页在线播放| 日本不卡高清中文字幕免费| 成人福利在线观看免费视频| 亚洲AV无码乱码国产麻豆| 999国内精品永久免费观看| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲人JIZZ日本人| 69xx免费观看视频| 精品一区二区三区免费毛片| 亚洲精品美女久久久久99| 亚洲无砖砖区免费| 精品久久久久亚洲| 亚洲AV无码久久| 日韩黄色免费观看| 中文字幕av免费专区| 亚洲中文字幕无码中文| 亚洲热妇无码AV在线播放| 成人毛片免费网站| 免费高清国产视频| 亚洲aⅴ无码专区在线观看|