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

singleton模式

webgou17年前 (2010-01-06)编程开发323
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模式” 的相关文章

GDB调试精粹及使用实例

一:列文件清单 1. List (gdb) list line1,line2 二:执行程序 要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符()和外壳通配符(*、?、[、])在内。 如果你使用不带参数的run命令,gdb就再次使用你给予前一…

Java正则表达式匹配、替换HTML内容( 转 )

正则表达式有一个很好用的工具--RegexBuddy,应用这个工具可以调度一个匹配你需要的正则表达式串,经过几番调度,把一些HTML标签的正则表达式匹配出来,接下来.....…

主流 AI 代码助手功能分析(2026年3月)

 一、市场概览AI 代码助手已从"补全工具"进化为"编程代理",核心趋势:从补全到 Agent:能自主完成完整功能、跑测试、提交 PRIDE 深度整合:Cursor、Windsurf 直接 fork VSCode多模型路由:GPT-4o、Claude…

手机游戏开发纹理图片优化心得

       之前写过一些纹理与内存优化的文章,本质说的就是图片格式的选用问题。这里记录一些最近开发中关于图片优化的经验和心得。       1、纹理压缩。  就像windows下的dds图片一样,使用纹理压缩…

Android屏幕大小相关技巧应用指南

Android应用程序中屏幕大小的设置大家应该都比较清楚,不过如何才能让屏幕自己适应环境而改变大小呢?在这里我们就可以为大家详细介绍一下有关Android屏幕大小的自适应方式,帮助大家理解。不同的Android target会有不同的大小,应用程序的界面需要针对不同的大小调整界面元素的尺寸。而且An…

iOS的多核编程和内存管理

这次的文章集中与iOS的多核编程和内存管理,为什么?因为iPad 2已经是双核CPU了!虽然iPad 1的应用已经不慢了,但大家完全可以使用苹果的多核编程框架来写出更加responsive的应用。多核运算在iOS中concurrency编程的框架就是GCD(Grand Central Dispatc…

发表评论

访客

看不清,换一张

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