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

singleton模式

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

阿里云ECS装docker+SS

 仅仅个人笔记记录下:首先主机选择ubuntu 14.04 64位的镜像,然后安装后apt-get update, apt-get -y upgrade。...…

How do I update Ubuntu Linux softwares?

Q. I have latest version of Ubuntu Linux. How do I update Ubuntu Linux for security and application fix or upgrades? A. Ubuntu Linux can be upgraded u…

WINCE下如何使用串口!

1.创建线程 //////////////////////////////////////////////////////////////////////////// DWORD CApplicationDlg::CommRecvTread(LPVOID lparam) { DWORD dwLeng…

第一个win32程序

#include "stdafx.h" LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstatn…

技术的问题,可用网站

很多问题是可以能过搜索引擎来找到答案的.今天看到一篇文章里面的网站很有用,转过来用了.…

Linux下的CPU利用率计算原理详解

在Linux/Unix下,CPU利用率分为用户态,系统态和空闲态,而CPU利用率是指 CPU执行非系统空闲进程的时间 / CPU总的执行时间。…

发表评论

访客

看不清,换一张

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