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

    主站蜘蛛池模板: 亚洲av伊人久久综合密臀性色 | 麻豆一区二区三区蜜桃免费| 亚洲国产精品无码av| 四虎永久在线精品免费网址| 巨胸喷奶水www永久免费| 亚洲人成网亚洲欧洲无码| 亚洲视频.com| 久久久无码精品亚洲日韩软件| 成人免费无码大片a毛片软件 | 日韩亚洲精品福利| 亚洲天堂免费在线| A级毛片高清免费视频在线播放| 免费福利资源站在线视频| 亚洲一区二区三区国产精华液| 色拍自拍亚洲综合图区| 最新国产AV无码专区亚洲| 又粗又黄又猛又爽大片免费| 成人人观看的免费毛片| 99无码人妻一区二区三区免费 | 久久精品亚洲福利| 国产zzjjzzjj视频全免费| 福利免费观看午夜体检区| 污视频在线免费观看| 在线毛片片免费观看| 色www永久免费| 99久久99这里只有免费的精品 | 不卡精品国产_亚洲人成在线| 卡一卡二卡三在线入口免费| 97性无码区免费| 5555在线播放免费播放| 久久免费视频99| 国产精成人品日日拍夜夜免费| a级毛片视频免费观看| 182tv免费视频在线观看| 成全视频高清免费观看电视剧| a级毛片毛片免费观看久潮喷| 久久久受www免费人成| 十八禁视频在线观看免费无码无遮挡骂过 | 久久久久久亚洲精品中文字幕| 亚洲精品和日本精品| 免费看国产精品麻豆|