設計模式

設計模式

設計模式代表了最佳實踐,是軟件開發過程中面臨一般問題的解決方案

設計模式是一套被反覆使用,經過分類,代碼設計的經驗

單例模式

單例模式也叫單件模式Singleton是一個非常用的設計模式,建一個線程安全且高效的Singleton是非常重要的

1.不考慮線程安全的一個單例模式

class Singleton
{
public:
	static Singleton* GetInstance()
	{
		if (_sInstance == NULL)
		{
			_sInstance = new Singleton();
		}
		return _sInstance;
	}
	static void DelInstance()
	{
		if (_sInstance)
		{
			delete _sInstance;
			_sInstance = NULL;
		}
	}
	void Print()
	{
		cout << _data << endl;
	}
private:
	//定義構造函數私有,限制只能在類內創建對象
	Singleton()
		:_data(0)
	{}
	Singleton(const Singleton&);
	Singleton& operator=(const Singleton&);
	//指向實例的指針爲靜態私有,這樣定義靜態成員函數獲取對象實例
	static Singleton* _sInstance;
	int _data;
};

2.線程安全單例

懶漢模式

class Singleton
{
public:
	static Singleton* GetInstance()
	{
        if(_sInstance==NULL)
        {
          lock();
		   if (_sInstance == NULL)
		   {
			_sInstance = new Singleton();
		   }
          unlock();
		   return _sInstance;
	}
	static void DelInstance()
	{
		if (_sInstance)
		{
			delete _sInstance;
			_sInstance = NULL;
		}
	}
	void Print()
	{
		cout << _data << endl;
	}
private:
	//定義構造函數私有,限制只能在類內創建對象
	Singleton()
		:_data(0)
	{}
	Singleton(const Singleton&);
	Singleton& operator=(const Singleton&);
	//指向實例的指針爲靜態私有,這樣定義靜態成員函數獲取對象實例
	static Singleton* _sInstance;
	int _data;
};
//靜態的數據成員必須初始化
Singleton* Singleton::_sInstance = NULL;
void TestSingleton()
{
	Singleton::GetInstance()->Print();
	Singleton::DelInstance();
}

第二個版本

class Singleton
{
public:
	static Singleton* GetInstance()
	{
                 
        
		if (_sInstance == NULL)
		{
			_sInstance = new Singleton();
		}
		return _sInstance;
	}
	static void DelInstance()
	{
	        std::lock_guard<std::mutex> lock(_mtx);
		if (_sInstance)
		{
			delete _sInstance;
			_sInstance = NULL;
		}
	}
	void Print()
	{
		cout << _data << endl;
	}
private:
	//定義構造函數私有,限制只能在類內創建對象
	Singleton()
		:_data(0)
	{}
	Singleton(const Singleton&);
	Singleton& operator=(const Singleton&);
	//指向實例的指針爲靜態私有,這樣定義靜態成員函數獲取對象實例
	static Singleton* _sInstance;
	int _data;
};
//靜態的數據成員必須初始化
Singleton* Singleton::_sInstance = NULL;
void TestSingleton()
{
	Singleton::GetInstance()->Print();
	Singleton::DelInstance();
}

餓漢模式

方式一

class Singleton
{
public:
// 獲取唯一對象實例的接口函數
      static Singleton* GetInstance()
     {
               static Singleton tmp;
               return &tmp;
    }
void Print()
{
          cout<<_data<<endl;
}
private:
// 構造函數定義爲私有,限制只能在類內創建對象
       Singleton()
           :_data(0)
        {}
    Singleton(const Singleton&);
   Singleton& operator=(const Singleton&);
// 單例類裏面的數據
   int _data;
};
void TestSingleton()
{
      Singleton::GetInstance()->Print();
}

方式二

class Singleton
{
public:
// 獲取唯一對象實例的接口函數
      static Singleton* GetInstance()
    {
        assert(_sInstance);
        return _sInstance;
     }
// 刪除實例對象
static void DelInstance()
{
if (_sInstance)
 {
       delete _sInstance;
     _sInstance = NULL;
}
}
void Print()
{
      cout << _data << endl;
}
private:
// 構造函數定義爲私有,限制只能在類內創建對象
Singleton()
   :_data(0)
   {}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
// 指向實例的指針定義爲靜態私有,這樣定義靜態成員函數獲取對象實例
static Singleton* _sInstance;
// 單例類裏面的數據
int _data;
};
Singleton* Singleton::_sInstance = new Singleton;
void TestSingleton()
{
   Singleton::GetInstance()->Print();
    Singleton::DelInstance();
}

在堆上創建對象

class AA
{
public:
	AA* GetObj()
	{
		return new AA;
	}
private:
	AA()
	{}
	AA(const AA&);
	int a;
};

在棧上創建對象

方式一

class AA
{
public:
	AA GetObj()
	{
		return  AA();
	}
private:
	AA()
	{}
	AA(const AA&);
	int a;
};

方式二

class AA
{
private:
	void*operator new(size_t);
	void operator delete(void*p);
	void*operator new[](size_t);
	void operator delete[](void* p);
private:
	AA()
	{}
	AA(const AA&);
	int a;
};


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