透明思考@CSDN

思考着的程序员,程序员的思考

用户操作
[即时聊天] [发私信] [加为好友]
透明@CSDNID:gigix
946839次访问,排名32好友0人,关注者13
gigix的文章
原创 361 篇
翻译 1 篇
转载 3 篇
评论 1816 篇
最近评论
sap99:www.sap99.com/,SAP99资料多多

SAP免费资料下载
http://www.sap99.com

有很多的学习资料,推荐一下,
shendl:public static AuthorizationService getInstance()

{

if(null == instance){

instance = new AuthorizationService();

}

return instanc……
lishali12345:你真的需要一直那些所谓的大师来摆弄吗?
我只是一个简单的读者而已,你总是拿一些所谓的名人大家的话来盖人,一个目的无非是想增加你自己说话的分量,其实你自己的话就压根没什么分量,基于对自己的不自信才会导致你在所有的文章中,开头以及结尾经常借大家之口来表达你要意淫的某些观点。
实在不忍心那些大家,经常就从你口之中说出来啊!
carry1002:你好,我是猎头公司carry,我们服务的对象主要是世界500强企业,现在有thougthtworks公司的职位机会,TW是敏捷方法领域的领头羊,有兴趣的朋友请和我联系,我的msn:carry.1@hotmail.com
zdonking:很好,感谢gigix前辈的经验分享。
文章分类
收藏
    相册
    我的图片
    测试
    Arrays.asList("Rod", "Jane", "Freddy");(RSS)
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Singleton模式的C++实现研究(示例代码)收藏

    新一篇: Singleton模式的C++实现研究(转贴) | 旧一篇: Singleton模式的C++实现研究(转贴)

    [附件一:演示程序代码清单]

    /*//////////////////////////////////////////////////////////////////

    作者:张友邦

    时间:2002年10月9日

    描述:实现Singleton

    /*//////////////////////////////////////////////////////////////////

     

    #include <iostream.h>

    #include <tchar.h>

     

    ////////////////////////////////////////////////////////////////////

    //第一种实现(使用模板函数)

    class MySingleton1

    {

    private:

        MySingleton1(){ cout << _T("Construct MySingleton1") << endl; }

        MySingleton1 & operator =(const MySingleton1&){}

        template <typename T>

        friend T& GetInstanceRef();

     

    public:

        ~MySingleton1(){ cout << _T("Destroy MySingleton1") << endl; }

     

    public:

        void DoSomething(){ cout << _T("Do something here in MySingleton1") << endl; }

    };

    template <typename T>

    T& GetInstanceRef()

    {

        static T _instance;

        return _instance;

    }

    template <typename T>

    T* GetInstancePtr()

    {

        return &GetInstanceRef<T>();

    }

    ////////////////////////////////////////////////////////////////////

     

    ////////////////////////////////////////////////////////////////////

    //第二种实现(使用模板类)

    template <typename T>

    class SingletonWraper

    {

    public:

        static T& GetInstanceRef()

        {

             static T _instance;

             return _instance;

        }

        static const T& GetInstanceConst()

        {

             return GetInstanceRef();

        }

        static T* GetInstancePtr()

        {

             return &GetInstanceRef();

        }

    };

    #define DEFINE_SINGLETON(ClassName); \

    public: \

        friend class SingletonWraper<ClassName>; \

        typedef class SingletonWraper<ClassName> SingletonWraper; \

        typedef SingletonWraper SingletonInterface; \

    private: \

        const ClassName& operator=(const ClassName&) \

        { \

             return SingletonInterface::GetInstanceRef(); \

        } \

        ClassName(const ClassName&); \

    private: \

        static void operator delete(void *p, size_t n) \

        { \

             throw -1; \

        }//End of define DECLARE_SINGLETON(ClassName);

     

    class MySingleton2

    {

        DEFINE_SINGLETON(MySingleton2);

    private:

        MySingleton2(){ cout << _T("Construct MySingleton2") << endl; }

     

    public:

        ~MySingleton2(){ cout << _T("Destroy MySingleton2") << endl; }

    public:

        void DoSomething(){ cout << _T("Do something here in MySingleton2") << "  " << endl; }

    };

    ////////////////////////////////////////////////////////////////////

     

    ////////////////////////////////////////////////////////////////////

    //第三种实现(由类自身实现,自动销毁对象,相比之下,它更简单)

    #define DECLARE_SINGLETON(ClassName); \

    public: \

        static ClassName& GetInstanceRef() \

        { \

             static ClassName _instance; \

             return _instance; \

        } \

        static const ClassName& GetInstanceConst() \

        { \

             return GetInstanceRef(); \

        } \

        static ClassName* GetInstancePtr() \

        { \

             return &GetInstanceRef(); \

        } \

        const ClassName& operator=(const ClassName&) \

        { \

             return GetInstanceRef(); \

        } \

    private: \

        ClassName(const ClassName&); \

        static void operator delete(void *p, size_t n) \

        { \

             throw -1; \

        }//End of define DECLARE_SINGLETON(ClassName);

     

    class MySingleton3

    {

        DECLARE_SINGLETON(MySingleton3);

     

    private:

        MySingleton3(){ cout << _T("Construct MySingleton3") << endl; ID = 0; }

     

    public:

        int ID;

        ~MySingleton3(){ cout << _T("Destroy MySingleton3") << endl; }

        void DoSomething(){ cout << _T("Do something here in MySingleton3, ID = ") << ID << endl; }

    };

    ////////////////////////////////////////////////////////////////////

     

    ////////////////////////////////////////////////////////////////////

    //第四种实现(《Design Patterns》里的,做了一些修改)

    //(由类自身实现,手动与自动销毁对象)

    #define ALLOW_SINGLETON(ClassName); \

    private: \

        static ClassName* _instance; \

     \

    public: \

        static ClassName& GetInstanceRef() \

        { \

             if (_instance == 0) \

                 _instance = new ClassName; \

             return *_instance; \

        } \

        static ClassName* GetInstancePtr() \

        { \

             return &GetInstanceRef(); \

        } \

        static ReleaseInstance() \

        { \

             if (_instance != 0) \

             { \

                 delete _instance; \

                 _instance = 0; \

             } \

        } //End of ALLOW_SINGLETON(ClassName);

     

    #define IMPLEMENT_SINGLETON(ClassName); \

    ClassName* ClassName::_instance = 0; \

    static class DestructHelper_##ClassName \

    { \

    public: \

        ~DestructHelper_##ClassName(){ ClassName::ReleaseInstance(); } \

    } DestructHelperInstance_##ClassName;

    //End of IMPLEMENT_SINGLE(ClassName);

     

    class MySingleton4

    {

    private:

        MySingleton4(){ cout << _T("Construct MySingleton4") << endl; } //构造函数私有

        ~MySingleton4(){ cout << _T("Destroy MySingleton4") << endl; } //析构函数放哪里都可以

        ALLOW_SINGLETON(MySingleton4);

     

    public:

        void DoSomething(){ cout << _T("Do something here in MySingleton4") << endl; }

    };

    IMPLEMENT_SINGLETON(MySingleton4);

    ////////////////////////////////////////////////////////////////////

     

    ////////////////////////////////////////////////////////////////////

    //测试

    void _tmain(int argc, char *argv[])

    {

        //测试第一种实现

        cout << _T("**************Test of the first implementation***************") << endl;

        MySingleton1* myobj1;

        myobj1 = GetInstancePtr<MySingleton1>();

        myobj1->DoSomething();

        GetInstanceRef<MySingleton1>().DoSomething();

     

        //测试第二种实现

        cout << endl << _T("**************Test of the second implementation**************") << endl;

        MySingleton2* myobj2;

        myobj2 = SingletonWraper<MySingleton2>::GetInstancePtr();

        myobj2->DoSomething();

        //MySingleton2 myobj22(*myobj2); //Error

        MySingleton2::SingletonInterface::GetInstanceRef().DoSomething();

     

        //测试第三种实现

        cout << endl << _T("**************Test of the third implementation***************") << endl;

        MySingleton3 *myobj3 = MySingleton3::GetInstancePtr();

        myobj3->ID = 1;

        myobj3->DoSomething();

        MySingleton3& myobj33 = MySingleton3::GetInstanceRef();

        myobj33 = *myobj3;

        try

        {

             delete myobj3;

        }

        catch(...)

        {

             cout << _T("Your object cannot be deleted.") << endl;

        }

        myobj33.ID = 2;

        myobj33.DoSomething();

        myobj3->DoSomething();

     

        //测试第四种实现

        cout << endl << _T("**************Test of the fourth implementation**************") << endl;

        MySingleton4 *myobj4 = MySingleton4::GetInstancePtr();

        myobj4->DoSomething();

        MySingleton4::GetInstanceRef().DoSomething();

     

        cout << _T("**********************End of all testing*********************") << endl << endl;

        cout << _T("Following is the Automatic Garbage Collection process:") << endl << endl;

    }

    ////////////////////////////////////////////////////////////////////

     

    [附件二:演示程序运行结果]

    **************Test of the first implementation***************

    Construct MySingleton1

    Do something here in MySingleton1

    Do something here in MySingleton1

     

    **************Test of the second implementation**************

    Construct MySingleton2

    Do something here in MySingleton2

    Do something here in MySingleton2

     

    **************Test of the third implementation***************

    Construct MySingleton3

    Do something here in MySingleton3, ID = 1

    Destroy MySingleton3

    Your object cannot be deleted.

    Do something here in MySingleton3, ID = 2

    Do something here in MySingleton3, ID = 2

     

    **************Test of the fourth implementation**************

    Construct MySingleton4

    Do something here in MySingleton4

    Do something here in MySingleton4

    **********************End of all testing*********************

     

    Following is the Automatic Garbage Collection process:

     

    Destroy MySingleton3

    Destroy MySingleton2

    Destroy MySingleton1

    Destroy MySingleton4

    发表于 @ 2002年10月24日 10:35:00|评论(loading...)|编辑

    新一篇: Singleton模式的C++实现研究(转贴) | 旧一篇: Singleton模式的C++实现研究(转贴)

    评论

    #Super 发表于2004-08-23 16:53:00  IP: 218.76.15.*
    厉害!佩服!
    #Super 发表于2004-08-23 17:21:00  IP: 218.76.15.*
    爱爱,你好厉害哦!辛苦你了!希望你继续下去!
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 透明@CSDN