关于SINGLTON在C++里实现(zz)

来源:互联网 发布:立诺软件 编辑:程序博客网 时间:2024/05/21 06:51
在《设计模式》中有一个叫做单件(Sigleton)的模式,是用来控制创建唯一对象。书中只讲到了如何建立Singleton对象
,对于如何来销毁此对象则只字不提。但是对象生命的管理对于C++程序员来说是多么的重要呀。或许Singleton只
是属于创建模式的一种,大师们认为在这里不应涉及到“销毁模式”。

有人认为Sinleton是应该在程序的退出的时候销毁的。但是退出应该是在什么时候呢。
请看如下代码:
假设是按设计模式里的方式来写一个对象Singlton对象。


class Singlton
{
private:
   
static Singlton * _insatnce;
    Singlton()
    {
        cout
<<"object is ["<<this<<"] Do< Construction"<<endl;
    }
public:
   
~Singlton()
    {
        cout
<<"object is ["<<this<<"] Do Destruction>"<<endl;
        _insatnce
= 0;
    }

   
static Singlton * GetSinglton()
    {
       
if(_insatnce)
           
return _insatnce;
    
        _insatnce
= new Singlton;
       
       
return _insatnce;
    }
   
void Dosomething()
    {
        cout
<<"object is ["<<this<<"] Do Something"<<endl;
    }
};

Singlton
* Singlton::_insatnce = NULL;

void foo(int i)
{
   
/*

        程序体
   
*/
   
if(i)
        Singlton::GetSinglton()
->Dosomething();
   
/*

        程序体
   
*/
}
int main()
{
   
/*
        void ;
        程序体
   
*/
    foo(
1)

//  if(Singlton::_insatnce) 
//       Singlton::GetSinglton(); 不能编译
    delete Singlton::GetSinglton();
}

事实上如果在Singlton某次运行根本就没有调用过foo(
1)而只是调用了foo(0),但是还必
须得在最后程序退出时调用实际上这时候调用GetSinglton()来建立对象马上就被删除了
。这是完全没有必要也是浪费的。想在程序执行时使用判断语句也是行不通的。这样的实
现还是可以改进的,使用在Singlton中再增加一个静态的成员函数CheckExistInstance来判
断对象是否存在,可以提高效率。但这样又给对象增加了接口,增加了代码维护的开销。

但是对象在程序结束时你并不清楚是不是真的不再需要此对象了。我们再修改代码如下。

class Singlton
{
private:
   
static Singlton * _insatnce;
    Singlton()
    {
        cout
<<"object is ["<<this<<"] Do< Construction"<<endl;
    }
public:
   
~Singlton()
    {
        cout
<<"object is ["<<this<<"] Do Destruction>"<<endl;
        _insatnce
= 0;
    }

   
static Singlton * GetSinglton()
    {
       
if(_insatnce)
           
return _insatnce;
    
        _insatnce
= new Singlton;
       
       
return _insatnce;
    }
   
void Dosomething()
    {
        cout
<<"object is ["<<this<<"] Do Something"<<endl;
    }
};

Singlton
* Singlton::_insatnce = NULL;

void foo(int i)
{
   
/*

        程序体
   
*/
   
if(i)
        Singlton::GetSinglton()
->Dosomething();
   
/*

        程序体
   
*/
}

class TestSingleton
{
public:
    TestSingleton()
    {
        Singlton::GetSinglton()
->Dosomething();
    }
   
~TestSingleton()
    {
        Singlton::GetSinglton()
->Dosomething();//此处出现内存泄露
    }
};
TestSingleton _test;
int main()
{
   
/*
        void ;
        程序体
   
*/
    foo(
1);

    delete Singlton::GetSinglton();
}

且看
~TestSingleton()申请出来的对象应该由谁来释放呢。由此引发了有人主张使用
引用记数器由模仿COM的Release来实现.实现代码如下
class Singleton
{
private:
   
static int m_Ref;
   
static Singleton * _instance;
public:
   
void DoSomething()
    {
        cout
<<"object is ["<<this<<"] Do Something"<<endl;
    }

   
static Singleton * GetSinglton()
    {
       
if(_instance)
        {
           
++m_Ref;
           
return _instance;
        }
        _instance
= new Singleton;
       
++m_Ref;
       
return _instance;
    }
   
    ULONG Release()
    {
       
--m_Ref;
       
if(0 == m_Ref)
        {
            delete _instance;
            _instance
= NULL;
           
return 0;
        }
       
return m_Ref;
    }
private:
    Singleton()
    {
        cout
<<"object is ["<<this<<"] Do< Construction"<<endl;
    }
   
~Singleton()
    {
        cout
<<"object is ["<<this<<"] Do Destruction>"<<endl;
    }
};
Singleton
*Singleton::_instance = NULL;
int Singleton::m_Ref = 0;


void foo()
{
    Singleton
* p = Singleton::GetSinglton();
    p
->DoSomething();
    p
->Release();
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    Singleton
* p = Singleton::GetSinglton();
    p
->DoSomething();
    p
->Release();

    foo();
   
return 0;
}

这样的方式是不存在内存泄露的,并且这段代码表面上是个单件;实际上,
Singleton对象是被多次建立和销毁的,如果这个对象不像以上代码写得那么简单,
是如果在单件中申请了不小的内存,那么以上的代码是多么的不可想象呀!更有如
果单件里记录了像使用次数那样的状态变量,那情况就更糟糕了。
事实上单件的实现并不比想像中的那样难。我们且看

class Singlton
{
private:
    Singlton()
    {
        cout
<<"object is ["<<this<<"] Do< Construction"<<endl;
    }
public:
   
~Singlton()
    {
        cout
<<"object is ["<<this<<"] Do Destruction>"<<endl;
    }

   
static Singlton & GetSinglton()
    {
       
static Singlton s;
       
return s;
    }
   
void Dosomething()
    {
        cout
<<"object is ["<<this<<"] Do Something"<<endl;
    }
};

void foo(int i)
{
   
/*

        程序体
   
*/
   
if(i)
        Singlton::GetSinglton().Dosomething();
   
/*

        程序体
   
*/
}
class TestSinglton
{
public:
    TestSinglton()
    {
    Singlton::GetSinglton().Dosomething();
    }
   
~TestSinglton()
    {
    Singlton::GetSinglton().Dosomething();
    }
};
TestSinglton test1;
int main()
{
   
/*
        void ;
        程序体
   
*/
    TestSinglton test2;
    foo(
1);
   
return 0;
}

这里用到的一个技巧就是使用了静态的变量,很明显有如下的好处:

1)如果在此次运行时根本没有用到单件,对像是不会被建立的。
2)需要用户来关心对象的释放
3)完全符合设计要求