<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 閱讀(331) 評論(0)  編輯  收藏 所屬分類: C&C++

    主站蜘蛛池模板: 国产高清对白在线观看免费91| 69视频在线观看免费| 成年性生交大片免费看| 青青青国产手机频在线免费观看 | 国产亚洲精品AA片在线观看不加载 | 国产乱子影视频上线免费观看| 亚洲AV日韩AV鸥美在线观看| 国产亚洲综合视频| 97性无码区免费| 亚洲s色大片在线观看| 阿v免费在线观看| 成人免费一区二区无码视频| 亚洲国产精品久久久久婷婷老年| 日韩一级片免费观看| 大陆一级毛片免费视频观看i| 日韩精品一区二区亚洲AV观看| 一区二区三区在线免费 | 亚洲国产精品尤物yw在线 | 亚洲国产综合人成综合网站| 中文字幕精品三区无码亚洲| 国产在线一区二区综合免费视频| 亚洲精品456播放| 久久精品国产亚洲av天美18| 欧美在线看片A免费观看| 在线免费观看亚洲| 国产亚洲免费的视频看| 亚洲午夜AV无码专区在线播放| 亚洲av永久无码天堂网| 国产在线国偷精品产拍免费| 亚洲成在人线中文字幕| 99热这里只有精品免费播放| 国产精品亚洲片在线| 久久99久久成人免费播放| 男人的天堂亚洲一区二区三区| 又大又粗又爽a级毛片免费看| 亚洲卡一卡二卡乱码新区| 国产精品视频免费| 久久综合亚洲鲁鲁五月天| 女人体1963午夜免费视频| 亚洲免费人成在线视频观看| 亚洲中文字幕久久无码|