C++ 單件模式實現及對象釋放

單件模式:

單件模式即在整個應用程序中只有一個類實例且這個實例所佔資源在整個應用程序中是共享的。

單件模式的C++實現(構造函數、拷貝構造函數、賦值操作符均需重寫):

#include <iostream>

class CSingleton
{
private:
    CSingleton()
    {
        std::cout<<"Singleton Constructed."<<std::endl;
    }
    CSingleton(const CSingleton&){};
    void operator =(const CSingleton&){};
    
    static CSingleton * _instance;
    class CGarbo 
    {
    public:
        ~CGarbo()
        {
            if (CSingleton::_instance)
            {
                delete CSingleton::_instance;
                std::cout<<"~CGarbo():Singleton Instance Deleted."<<std::endl;
            }
            CSingleton::_instance = NULL;
        }
    };
    friend class CGarbo;

public:
    ~CSingleton()
    {
        std::cout<<"~CSingleton():Singleton Destructed."<<std::endl;
    }
    static CSingleton * instance()
    {
        if(!_instance)
        {
            _instance = new CSingleton();
            static CGarbo _garbo;
        }
        return _instance;
    }
    
    ////for test
    void test(){
        std::cout<<"TEST()"<<std::endl;
    }
};

CSingleton *CSingleton::_instance=NULL;

int main()
{
    CSingleton::instance() ->test();
    return 0;
}

單件模式的宏定義實現:

使用宏定義實現的好處在於:

1、代碼更清晰;

2、當一個系統中需要用到多個單件模式的實例時,可重用代碼;

#include <iostream>

#define PATTERN_SINGLETON_DECLARE(classname) \
private:                                    \
    static classname * _instance;           \
    class CGarbo                            \
    {                                       \
    public:                                 \
        ~CGarbo()                           \
        {                                   \
            if (classname::_instance)       \
            {                               \
                delete classname::_instance;\
                std::cout<<"~CGarbo():Singleton Instance Deleted."<<std::endl;\
            }                               \
            classname::_instance = NULL;    \
        }                                   \
    };                                      \
    friend class CGarbo;                    \
public:                                     \
    static classname* instance();
    
#define PATTERN_SINGLETON_IMPLEMENT(classname)   \
classname * classname::_instance=NULL;          \
classname * classname::instance()               \
{                                               \
    if(!_instance)                              \
    {                                           \
        _instance = new classname();            \
        static CGarbo _garbo;                   \
    }                                           \
    return _instance;                           \
}

class CSingleton
{
    PATTERN_SINGLETON_DECLARE(CSingleton);
    
private:
    CSingleton()
    {
        std::cout<<"Singleton Constructed."<<std::endl;
    }
    CSingleton(const CSingleton&){};
    void operator =(const CSingleton&){};
    
public:
    ~CSingleton()
    {
        std::cout<<"~CSingleton():Singleton Destructed."<<std::endl;
    }
    
    
    ////for test
    void test(){
        std::cout<<"TEST()"<<std::endl;
    }
};


//全局共享,所有接口均通過該宏來訪問
#define g_Singleton (*CSingleton::instance())

PATTERN_SINGLETON_IMPLEMENT(CSingleton);

int main()
{
    g_Singleton.test();
    return 0;
}
具體實現時,以下部分一般在cpp文件中實現,其他在.h中進行聲明:

PATTERN_SINGLETON_IMPLEMENT(CSingleton);

int main()
{
    g_Singleton.test();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章