2009年5月4日星期一

singleton设计模式

先看一个最简单的Singleton

class Singleton1
{
public:
Singleton1& Instance()
{
static Singleton Obj;
return Obj;

private:
Singleton1();
}
}
优雅的解决了问题。

再看一个可扩展的。


#include
#include
class SingletonBase
{
public:
SingletonBase() { cout << "SingletonBase created!" << endl; }

virtual ~SingletonBase() { cout << "SingletonBase destroyed!" << endl; }

virtual void Access() { cout << "SingletonBase accessed!" << endl; }

static SingletonBase* GetObj() { return m_pObj; }

static void SetObj(SingletonBase* pObj) { m_pObj = pObj; }

protected:
static SingletonBase* m_pObj;

};

SingletonBase* SingletonBase::m_pObj;

inline SingletonBase* Base()
{
assert(SingletonBase::GetObj());
return SingletonBase::GetObj();
}
// Create a derived singleton-type class
class SingletonDerived : public SingletonBase
{
public:
SingletonDerived()
{ cout << "SingletonDerived created!" << endl; }

virtual ~SingletonDerived()
{ cout << "SingletonDerived destroyed" << endl; }

virtual void Access()
{ cout << "SingletonDerived accessed!" << endl; }

protected:

};

inline SingletonDerived* Derived()
{
assert(SingletonDerived::GetObj());
return (SingletonDerived*)SingletonDerived::GetObj();
}
// Using the code...
// The complex singleton requires more work to use, but is
// more flexible. It also allows more control over object
// creation, which is sometimes desirable.

SingletonDerived::SetObj(new SingletonDerived);

// Notice that the functionality has been overridden by the new
// class, even though accessing it through the original method.

Base()->Access();
Derived()->Access();

// This variation on a singleton unfortunately requires both
// explicit creation and deletion.

delete SingletonDerived::GetObj();

一个可扩展的singleton。
主要用于需要大量访问的全局对象。

没有评论: