




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、singleton模式是常用的设计模式之一, 但是要实现一个真正实用的设计模式却也不是件容易的事情。1.标准的实现class singletonpublic:static singleton * instance()if(0= _instance) _instance = new singleton;return _instance;protected: singleton(void)virtual singleton(void)static singleton* _instance;这是教科书上使用的方法。 看起来没有什么问题, 其实包含很多的问题。下面我们一个一个的解决。2.自动垃圾回收上
2、面的程序必须记住在程序结束的时候,释放内存。 为了让它自动的释放内存,我们引入 auto_ptr改变它。#include#includeusingnamespace std ;class singletonpublic:static singleton * instance()if(0= _instance.get () _instance.reset( new singleton);return _instance.get ();protected: singleton(void) cout create singleton endl;virtual singleton(void) cout
3、 destroy singletonendl ;friendclass auto_ptr;static auto_ptr _instance;/singleton.cpp auto_ptr singleton: _instance;3.增加模板在我的一个工程中,有多个的singleton类,对 singleton类,我都要实现上面这一切,这让我觉得烦死了。 于是我想到了模板来完成这些重复的工作。现在我们要添加本文中最吸引人单件实现:/* (c) 2003-2005 c2217 studio module: singleton.h author: yangjun d. created: 9/3/
4、2005 23:17 purpose: implement singleton pattern history:*/#pragmaonce#includeusingnamespace std ;usingnamespace c2217: win32;namespace c2217namespace patterntemplateclass singletonpublic:staticinline t* instance();private: singleton(void) singleton(void) singleton(const singleton&) singleton &am
5、p; operator=(const singleton &)static auto_ptr _instance;templateauto_ptr singleton: _instance;templateinline t * singleton: instance()if(0 = _instance.get () _instance.reset ( new t );return _instance.get ();/class that will implement the singleton mode,/must use the macro in its delare file#de
6、fine declare_singleton_class( type )friendclass auto_ptr;friendclass singleton;4.线程安全上面的程序可以适应单线程的程序。但是如果把它用到多线程的程序就会发生问题。主要的问题在于同时执行_instance.reset ( new t ); 就会同时产生两个新的对象,然后马上释放一个,这跟singleton模式的本意不符。所以,你需要更加安全的版本:/* (c) 2003-2005 c2217 studio module: singleton.h author: yangjun d. created: 9/3/200
7、5 23:17 purpose: implement singleton pattern history:*/#pragmaonce#includeusingnamespace std ;#includeinterlocked.husingnamespace c2217: win32;namespace c2217namespace patterntemplateclass singletonpublic:staticinline t* instance();private: singleton(void) singleton(void) singleton(const singleton&a
8、mp;) singleton & operator=(const singleton &)static auto_ptr _instance;static cresguard _rs;templateauto_ptr singleton: _instance;templatecresguard singleton: _rs ;templateinline t * singleton: instance()if(0 = _instance.get () cresguard: cguard gd( _rs );if(0= _instance.get () _instance.res
9、et ( new t );return _instance.get ();/class that will implement the singleton mode,/must use the macro in its delare file#define declare_singleton_class( type )friendclass auto_ptr;friendclass singleton;cresguard 类主要的功能是线程访问同步,代码如下:/*module: interlocked.hnotices: copyright (c) 2000 jeffrey richter*/
10、#pragmaonce/ instances of this class will be accessed by multiple threads. so,/ all members of this class (except the constructor and destructor)/ must be thread-safe.class cresguard public: cresguard() m_lgrdcnt =0 ; initializecriticalsection(& m_cs); cresguard() deletecriticalsection(& m_c
11、s);/ isguarded is used for debugging bool isguarded()constreturn(m_lgrdcnt 0);public:class cguard public: cguard( cresguard& rg ) : m_rg( rg ) m_rg.guard(); cguard() m_rg.unguard();private: cresguard& m_rg;private:void guard() entercriticalsection(& m_cs); m_lgrdcnt+;void unguard() m_lgr
12、dcnt-; leavecriticalsection(& m_cs);/ guard/unguard can only be accessed by the nested cguard class.friendclass cresguard: cguard;private: critical_section m_cs;long m_lgrdcnt;/ # of entercriticalsection calls;/5.实用方法比如你有一个需要实现单件模式的类,就应该这样实现:#pragmaonce#includesingleton.husingnamespace c2217: pattern;class servicemangerpublic:void run ()private: servicemanger(void)virtual servicemanger(void) declare_singleton_class( servicemanger);typedef singleton ssmanger;在使用的时候很简单,跟一般的s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《非全日制劳动合同》模板
- 物业管理企业秩序维护工作手册范本
- 校长在教育教学质量提升经验交流会上的发言:从一节课的变化看教育质量的成长
- 幽门螺杆菌课件提问
- 2025年口腔行业投放分析报告-培训课件
- 巡察检查工作要点课件
- 峡山区安全培训班课件
- 尾气烟囱施工安全培训
- 小鸭找家课件
- 励志教育做一只努力向上的蜗牛主题班会
- 一、长方体和正方体表面涂色的
- 《广播电视编导概论》课程教学大纲
- kinetix6200和6500模块化多轴伺服驱动器用户手册
- DB51∕T 2502-2018 中国川菜烹饪技术用语及菜名翻译规范
- 国外期刊运作的主要模式及发展趋势
- 区域性再生资源集散市场实施方案
- 液氨使用与储存安全技术规范
- 《幼儿园大班第一学期家长会》 PPT课件
- 施工手册柱式桥台
- PCR室作业指导书_检验SOP文件
- 上海市初级中学英语学科教学基本要求
评论
0/150
提交评论