当前位置:首页 > 编程开发

singleton模式

webgou17年前 (2010-01-06)编程开发117
singleton模式,些模式变体有几种,这种基本实现是在单线程中实现的 c++ code: #include "stdio.h" class singleton { static singleton *instance;//不能在这里初始化 singleton(){} ~singleton(){} public: static singleton * getInstance() { if(instance == NULL) instance = new singleton; return instance ; } }; singleton* singleton::b = NULL; //静态成员初始化, 不初始化会出错 void main() { singleton *b = singleton::getInstance(); if(b == 0) puts("ww"); } java code: public class singleton { private static singleton instance = new singleton (); private singleton(){} // java 中不存在~singleton(){} public static singleton getInstance() { return instance; } }; 另外的代码: #ifndef _SINGLETON_H_ #define _SINGLETON_H_ #include #include /******************************************************************** created: 2010/01/06 created: 6:1:2010 16:57 file base: singleton file ext: h author: lizp purpose: singleton练习 *********************************************************************/ class Singleton { private: Singleton() { cout<<"create singleton!" <; static auto_ptr instance; }; auto_ptr Singleton::instance; } namespace auto_prt_template { #if 0 template class Singleton { protected: Singleton() { cout<<"construct auto_prt_template"< instance; }; #endif class CResGuard; template class Singleton { protected: Singleton() { cout<<"construct auto_prt_template"< instance; static CResGuard m_rg; }; template auto_ptr Singleton::instance; template CResGuard Singleton::m_rg; #define DECLARE_CLASS_SINGLETON(type) \ friend auto_ptr;\ friend Singleton; class CManage_service { public: void doSomething() { cout<<"do something"< slMange; class CResGuard { public: CResGuard(){m_lGrdCnd = 0; InitializeCriticalSection(&m_cs);} ~CResGuard(){DeleteCriticalSection(&m_cs);} bool IsGuard()const {return (m_lGrdCnd>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(){m_lGrdCnd++;EnterCriticalSection(&m_cs);} void unGuard(){LeaveCriticalSection(&m_cs);m_lGrdCnd--;} private: friend class CResGuard::CGuard; CRITICAL_SECTION m_cs; long m_lGrdCnd; }; } Singleton* Singleton::instance; #endif 使用代码: // gof.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Handler.h" #include "singleton.h" int main(int argc, char* argv[]) { /************************************************************************/ /* chain of responsibility */ /************************************************************************/ CHandler *ch1 = new CConcreteHandler1(); CHandler *ch2 = new CConcreteHandler2(); CHandler *ch3 = new CConcreteHandler3(); ch2->successor(ch1); ch3->successor(ch2); ch2->HandleRequest(); ch3->HandleRequest(); /************************************************************************/ /* singleton */ /************************************************************************/ Singleton *s = Singleton::getInstance(); auto_prt_singleton::Singleton *as = auto_prt_singleton::Singleton::getInstance(); auto_prt_template::CManage_service *ast = auto_prt_template::slMange::getInstance(); system("pause"); return 0; }

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=107

标签: 模式
分享给朋友:

“singleton模式” 的相关文章

vi/vim 操作图

vi/vim 操作图…

Android消息传递应用功能解析

Android手机操作系统中有一种叫做Intent的消息传递机制。这在实际编程中是一个核心技术,值得我们去深入的研究。在这里大家将会了解到有关Android消息传递的一些基础应用技巧,帮助大家理解。每一个Cursor、ContentResolver做为一个小的注册中心,相关观察者可以在这个中心注册,…

总线标准

微机中总线一般分为内部总线,系统总线,外部总线.内部总线是微机内部各外围芯片与处理器之间的总线,用于芯片的一级互联;而系统总线是各个插板与系统板之间的总线,用于插件板的一级互连;外部总线是微机和外部之间的总线,微机做为一种设备通过该总线和其它设备进行信息和数据交换,它用于设备一级的互连.从广义上说,…

解读iPhone平台的一些优秀设计思路

解读iPhone平台的一些优秀设计思路 …

rpm 用法简介

rpm可谓是Redhat公司的一大贡献,它使Linux的软件安装工作变得更加简单容易。 1. 安装: 我只需简单的一句话,就可以说完。执行:rpm ivh rpm软件包名rpm参数 参数说明-i 安装软件-t 测试安装,不是真的安装-p 显示安装进度-f 忽略任何错误-U 升级安装-v 检测套件是否…

查看MYSLQ版本号

直接在MYSQL 命令行输入:'\S'…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。