Singleton就是保證一個(gè)類(lèi)只有一個(gè)實(shí)例,在Ogre里面有很多Singleton的例子,幾乎所有的Manager都是Singleton的實(shí)例。比較下Gof里面的Singleton的實(shí)現(xiàn)和loki中的Singleton的實(shí)現(xiàn),Ogre的Singleton算是比較簡(jiǎn)單的。Gof中實(shí)現(xiàn)一般是把類(lèi)的構(gòu)造函數(shù)給Private了然后再加上static的變量和函數(shù)實(shí)現(xiàn)的。下面來(lái)具體看一下Ogre中的Singleton
#include <iostream>
#include "singleton.h"
class Intsin:public Singleton< Intsin >


{
public:

Intsin(int tmp):t(tmp)
{std::cout << "build"<< std::endl;}

~Intsin()
{std::cout << "destory"<< std::endl;}

print()
{std::cout << t << std::endl;}
private:
int t;
};

template<> Intsin* Singleton< Intsin > ::ms_Singleton = 0;

int main()


{
Intsin* a;
a = new Intsin(3);
a->print();
delete a;
system("pause");
}
上面是一個(gè)超簡(jiǎn)單的實(shí)現(xiàn),主要來(lái)看一下a = new Intsin(3);這里系統(tǒng)先按照Intsin的大小分配一塊內(nèi)存出來(lái),然后調(diào)用Intsin的構(gòu)造函數(shù),當(dāng)然要先運(yùn)行基類(lèi)Singleton<Intsin>的構(gòu)造函數(shù),這時(shí)Singleton構(gòu)造函數(shù)的this指針應(yīng)該是指向剛才分配的那個(gè)Intsin對(duì)象的地址了,雖然里面的東西不完整,但是this指向的對(duì)象的基本框架已經(jīng)有了。所以可以這樣使用它。要想知道這時(shí)this指針指向的對(duì)象到底有什么東西,什么東西沒(méi)有,可以搜索一下“構(gòu)造函數(shù)的this”看一下有關(guān)文章。當(dāng)構(gòu)造函數(shù)完成的時(shí)候,new返回一個(gè)指針。這樣就完成了一個(gè)實(shí)例了。
下面來(lái)說(shuō)一下,這個(gè)類(lèi)什么東西沒(méi)做,用的時(shí)候要注意什么。
1.當(dāng)要實(shí)例化一個(gè)已經(jīng)被實(shí)例化的類(lèi)時(shí),會(huì)有assert(!ms_Singleton);所以一定不能這樣做。
2.對(duì)一個(gè)實(shí)例的指針兩次調(diào)用delete也是讓程序崩潰的好辦法
3.Singleton<>不負(fù)責(zé)對(duì)象的解析,所以一定要記得自己delete,記住就一次。
總結(jié)下,這里的Singleton的實(shí)現(xiàn)比較簡(jiǎn)單,也很好用,但是程序員要負(fù)責(zé)的東西比較多。在Ogre中幾乎所有的Singleton對(duì)象都是在root里面new和delete的。
就這么多吧。
1
#ifndef _SINGLETON_H__
2
#define _SINGLETON_H__
3
4
namespace Ogre
{
5
template <typename T> class Singleton
6
{
7
protected:
8
static T* ms_Singleton;
9
public:
10
Singleton( void )
11
{
12
assert( !ms_Singleton );
13
#if defined( _MSC_VER ) && _MSC_VER < 1200
14
int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
15
ms_Singleton = (T*)((int)this + offset);
16
#else
17
ms_Singleton = static_cast< T* >( this );
18
#endif
19
}
20
~Singleton( void )
21
{ assert( ms_Singleton ); ms_Singleton = 0; }
22
static T& getSingleton( void )
23
{ assert( ms_Singleton ); return ( *ms_Singleton ); }
24
static T* getSingletonPtr( void )
25
{ return ms_Singleton; }
26
};
27
}
28
#endif

2

3

4



5

6



7

8

9

10

11



12

13

14

15

16

17

18

19

20

21



22

23



24

25



26

27

28

先來(lái)看一下Singleton的構(gòu)造函數(shù)是Public的,這樣不是大家都能用了?往下看,第一句就是assert(!ms_Singleton); 如果ms_Singleton不是0的話,也就是Singleton<T>已經(jīng)有了一個(gè)自己的對(duì)象,那么程序就死翹翹了。如果是0,那么跳過(guò)13-16行,直接看17行的
ms_Singleton = static_cast<T*>(this),有沒(méi)有注意到那個(gè)this,出現(xiàn)在這里有點(diǎn)詭異。這里是構(gòu)造函數(shù),對(duì)象的初始化還沒(méi)有完成呢,那么this指向的東西是什么呢?先來(lái)看一下怎么生成一個(gè)Singleton的實(shí)例。
































下面來(lái)說(shuō)一下,這個(gè)類(lèi)什么東西沒(méi)做,用的時(shí)候要注意什么。
1.當(dāng)要實(shí)例化一個(gè)已經(jīng)被實(shí)例化的類(lèi)時(shí),會(huì)有assert(!ms_Singleton);所以一定不能這樣做。
2.對(duì)一個(gè)實(shí)例的指針兩次調(diào)用delete也是讓程序崩潰的好辦法
3.Singleton<>不負(fù)責(zé)對(duì)象的解析,所以一定要記得自己delete,記住就一次。
總結(jié)下,這里的Singleton的實(shí)現(xiàn)比較簡(jiǎn)單,也很好用,但是程序員要負(fù)責(zé)的東西比較多。在Ogre中幾乎所有的Singleton對(duì)象都是在root里面new和delete的。
就這么多吧。
posted @ 2008-11-19 11:07 空心菜 閱讀(2464) | 評(píng)論 (1) | 編輯 收藏