首页 > 其他 > 详细

Singleton

时间:2015-07-04 02:01:20      阅读:281      评论:0      收藏: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;
}

Singleton

原文:http://www.blogjava.net/bacoo/archive/2015/07/03/426021.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!