<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丁香亚洲综合社区| 特黄特色的大片观看免费视频| 成人免费无码大片A毛片抽搐色欲| 久久精品蜜芽亚洲国产AV| 日韩免费观看一区| 亚洲精品制服丝袜四区| 日韩精品无码免费专区午夜| 国产l精品国产亚洲区在线观看| 久久久久女教师免费一区| 亚洲日韩精品一区二区三区| a在线免费观看视频| 精品亚洲国产成AV人片传媒| 精品久久8x国产免费观看| 亚洲成_人网站图片| 国产精品自在自线免费观看| 特级毛片A级毛片100免费播放| 亚洲国产成人五月综合网| 丁香花在线观看免费观看图片 | 在线观看视频免费国语| 亚洲av永久中文无码精品| 免费v片在线观看无遮挡| 成人网站免费大全日韩国产| 亚洲av永久无码制服河南实里| 2021国内精品久久久久精免费| 亚洲大片免费观看| 日本特黄特黄刺激大片免费| 一级做a爰片久久毛片免费看| 亚洲人成无码网站| 成人免费视频69| 男男黄GAY片免费网站WWW| 亚洲V无码一区二区三区四区观看 亚洲αv久久久噜噜噜噜噜 | 天天看免费高清影视| 无码免费又爽又高潮喷水的视频 | 亚洲国产a级视频| 久久精品电影免费动漫| 自拍日韩亚洲一区在线| 亚洲午夜国产片在线观看| 51视频精品全部免费最新| 色婷婷亚洲一区二区三区| 亚洲女初尝黑人巨高清|