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

singleton模式

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

ServerSocketChannel

http://diary.51.com/diary/diary_add.php?groupid=103706呵呵,我来告诉你吧,至于例子代码在我的回答记录中有。你自己翻来去看,是用nio给一个学生写的BBS聊天室。在用nio通讯的过程我用以下情景给你模拟: 1. 学校(ServerSocketCha…

C# + Socket断线重连

 一、网上常用方法...…

Android类库常用类型解析

在Android类库中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型、提供基本的运行环境android.content 包含各种的对设备上的数据进行访问和发布的类android.database :通过内容提供者浏览和操作数据库android…

WINCE下如何使用串口!

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

SOAP协议规范

SOAP协议规范1. 简介SOAP 以XML形式提供了一个简单、轻量的用于在分散或分布环境中交换结构化和类型信息的机制。SOAP本身并没有定义任何应用程序语义,如编程模型或特定语义的实现;实际上它通过提供一个有标准组件的包模型和在模块中编码数据的机制,定义了一个简单的表示应用程序语义的机制。这使SO…

Amplify Shader Editor Beginner – 入門篇

 過往在編寫著色器效果時,都必須要透過大量的電腦圖學、數學計算、渲染流程…等知識來輔佐我們,也因為需要這麼多的事前準備,導致學習著色器的門檻相對較高,讓許多程式、非程式人員不敢輕易的轉動這扇門的把手。而在前些日子中,偶然注意到 Unity 官方所推薦的這一套可視化著色器編輯器…

发表评论

访客

看不清,换一张

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