查看完整版本: C++中单例模式对象的释放控制

今天我做东 2008-4-14 07:00

C++中单例模式对象的释放控制

[font=宋体][size=12pt]  单例模式也称为单件模式、单子模式。使用单例模式,保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享。有很多地方需要这样的功能模块,如系统的日志输出等。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  单例模式有许多种实现方法,在[/size][/font][size=12pt]C++[/size][font=宋体][size=12pt]中,甚至可以直接用一个全局变量做到这一点,但这样的代码显得很不优雅。《设计模式》一书中给出了一种很不错的实现,定义一个单例类,使用类的私有静态指针变量指向类的唯一实例,并用一个公有静态方法获取该实例。如下面的类定义:[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  [/size][/font]
[font=宋体][size=12pt]以下是引用片段:[/size][/font][size=12pt][/size]
[size=12pt][/size]
[size=12pt]class CSingleton:[/size]
[size=12pt]{[/size]
[size=12pt]
// [/size][font=宋体][size=12pt]其它成员[/size][/font][size=12pt][/size]
[size=12pt]public:[/size]
[size=12pt]
static CSingleton * GetInstance()[/size]
[size=12pt]
{[/size]
[size=12pt]
if (m_pInstance == NULL)[/size]
[size=12pt]
m_pInstance = new CSingleton();[/size]
[size=12pt]
return m_pInstance;[/size]
[size=12pt]
}[/size]
[size=12pt]
[/size]
[size=12pt]private:[/size]
[size=12pt]
CSingleton(){};[/size]
[size=12pt]
static CSingleton * m_pInstance;[/size]
[size=12pt]} [/size]
[font=宋体][size=12pt]  单例类[/size][/font][size=12pt]CSingleton[/size][font=宋体][size=12pt]有以下特征:[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  它有一个指唯一实例的静态指针[/size][/font][size=12pt]m_pInstance[/size][font=宋体][size=12pt],并且是私有的。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  它有一个公有的函数,可以获取这个唯一的实例,并在需要的时候创建该实例。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  它的构造函数是私有的,这样就不能从别处创建该类的实例。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  大多时候,这样的实现都不会出现问题。有经验的读者可能会问,[/size][/font][size=12pt]m_pInstance[/size][font=宋体][size=12pt]指向的空间什么时候释放呢[/size][/font][size=12pt]?[/size][font=宋体][size=12pt]更严重的问题是,这个实例的析构操作什么时候执行[/size][/font][size=12pt]?[/size]
[size=12pt][/size]
[font=宋体][size=12pt]  如果在类的析构行为中有必须的操作,比如关闭文件,释放外部资源,那么上面所示的代码无法实现这个要求。我们需要一种方法,正常地删除该实例。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  可以在程序结束时调用[/size][/font][size=12pt]GetInstance[/size][font=宋体][size=12pt]并对返回的指针调用[/size][/font][size=12pt]delete[/size][font=宋体][size=12pt]操作。这样做可以实现功能,但是不仅很丑陋,而且容易出错。因为这样的附加代码很容易被忘记,而且也很难保证在[/size][/font][size=12pt]delete[/size][font=宋体][size=12pt]之后,没有代码再调用[/size][/font][size=12pt]GetInstance[/size][font=宋体][size=12pt]函数。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  一个妥善的方法是让这个类自己知道在合适的时候把自己删除。或者说把删除自己的操作挂在系统中的某个合适的点上,使其在恰当的时候自动被执行。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  我们知道,程序在结束的时候,系统会自动析构所有的全局变量。事实上,系统也会析构所有的类的静态成员变量,就像这些静态成员也是全局变量一样。利用这个特征,我们可以在单例类中定义一个这样的静态成员变量,而它的唯一工作就是在析构函数中删除单例类的实例。如下面的代码中的[/size][/font][size=12pt]CGarbo[/size][font=宋体][size=12pt]类[/size][/font][size=12pt](Garbo[/size][font=宋体][size=12pt]意为垃圾工人[/size][/font][size=12pt])[/size][font=宋体][size=12pt]:[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  [/size][/font]
[font=宋体][size=12pt]以下是引用片段:[/size][/font][size=12pt][/size]
[size=12pt][/size]
[size=12pt][/size]
[size=12pt]class CSingleton:[/size]
[size=12pt]{[/size]
[size=12pt]
// [/size][font=宋体][size=12pt]其它成员[/size][/font][size=12pt][/size]
[size=12pt]public:[/size]
[size=12pt]
static CSingleton * GetInstance(){[/size][font=宋体][size=12pt]。。。[/size][/font][size=12pt]}[/size]
[size=12pt]private:[/size]
[size=12pt]
CSingleton(){};[/size]
[size=12pt]
static CSingleton * m_pInstance;[/size]
[size=12pt]
[/size]
[size=12pt]
class CGarbo // [/size][font=宋体][size=12pt]它的唯一工作就是在析构函数中删除[/size][/font][size=12pt]CSingleton[/size][font=宋体][size=12pt]的实例[/size][/font][size=12pt][/size]
[size=12pt]
{[/size]
[size=12pt]
public:[/size]
[size=12pt]
~CGarbo()[/size]
[size=12pt]
{[/size]
[size=12pt]
if (CSingleton::m_pInstance)[/size]
[size=12pt]
delete CSingleton::m_pInstance;[/size]
[size=12pt]
}[/size]
[size=12pt]
};[/size]
[size=12pt]
[/size]
[size=12pt]
static CGarbo Garbo; // [/size][font=宋体][size=12pt]定义一个静态成员,在程序结束时,系统会调用它的析构函数[/size][/font][size=12pt][/size]
[size=12pt]}[/size]
[size=12pt]
[/size]
[size=12pt]
[/size]
[font=宋体][size=12pt]  类[/size][/font][size=12pt]CGarbo[/size][font=宋体][size=12pt]被定义为[/size][/font][size=12pt]CSingleton[/size][font=宋体][size=12pt]的私有内嵌类,以防该类被在其它地方滥用。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  在程序运行结束时,系统会调用[/size][/font][size=12pt]CSingleton[/size][font=宋体][size=12pt]的静态成员[/size][/font][size=12pt]Garbo[/size][font=宋体][size=12pt]的析构函数,该析构函数会删除单例的唯一实例。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  使用这种方法释放单例对象有以下特征:[/size][/font][size=12pt][/size]
[size=12pt][/size]
[font=宋体][size=12pt]  在单例类内部定义专有的嵌套类。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[size=12pt]
[/size][font=宋体][size=12pt]在单例类内定义私有的专门用于释放的静态成员。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[size=12pt]
[/size][font=宋体][size=12pt]利用程序在结束时析构全局变量的特性,选择最终的释放时机。[/size][/font][size=12pt][/size]
[size=12pt][/size]
[size=12pt]
[/size][font=宋体][size=12pt]使用单例的代码不需要任何操作,不必关心对象的释放。[/size][/font]

[[i] 本帖最后由 今天我做东 于 2008-4-14 07:01 编辑 [/i]]

momo.s 2008-4-20 16:43

要顶就要顶这类贴子嘛!!!

momotea 2008-4-27 11:03

这贴,不顶就过意不去,呵呵~~~

仟如昵 2008-11-5 20:10

英语学习 免费

[size=5][color=sienna]超分贝英语!
[/color][/size][url=http://www.yienglish.com/][size=5][color=sienna]www.yiEnglish.com[/color][/size][/url][size=5][color=sienna]  

所学到将不仅仅是非常纯正、地道的英语![/color][/size]

yojoyui9 2008-11-15 03:48

顶你个肺

哈哈!路过,帮顶~~~~~
















-------------------------------------------------------------------------------------------------------------------------------------
[size=2][color=silver]个性签名:[/color][/size][url=http://blog.sina.com.cn/dghgm][color=silver]寻仙外挂[/color][/url][size=2][color=silver].[/color][/size][url=http://blog.sina.com.cn/jxsjwgxz][size=2][color=silver]剑侠世界免费外挂[/color][/size][/url][size=2][color=silver].[/color][/size][url=http://www.wzxinji.com.cn][size=2][color=silver]剑侠世界外挂[/color][/size][/url][size=2][color=silver].[/color][/size][url=http://www.wlkji.cn][size=2][color=silver]传奇外传外挂[/color][/size][/url][size=2][color=silver].[/color][/size][url=http://www.hrxjewel.com.cn][size=2][color=silver]热血江湖外挂[/color][/size][/url][size=2][color=silver]![/color][/size]
页: [1]
查看完整版本: C++中单例模式对象的释放控制
5.12四川大地震