三种单例模式的C++实现
来源:互联网 发布:缩略图软件下载 编辑:程序博客网 时间:2024/06/05 06:51
通过单例模式,可以做到以下三点:
1.确保一个类只有一个实例被创建
2.提供了一个对对象全局访问的指针
3.在不影响单例类的客户端的情况下允许将来有多个实例
延迟式:
特点:延迟加载,也就是说直到实力类被用到的时候才会被加载
C++代码:
Singleton.h#ifndef __C__Review__Singleton__#define __C__Review__Singleton__#include <iostream>class Singleton{private: Singleton() { } Singleton(const Singleton&);//⽆无实现 Singleton& operator=(const Singleton&); //⽆无实现 static Singleton *instance; public: static Singleton *getInstance(); static void release();};#endif /* defined(__C__Review__Singleton__) */Singleton.cpp#include "Singleton.h"Singleton *Singleton::instance = 0;Singleton* Singleton::getInstance(){ if(instance == nullptr) instance = new Singleton(); return instance;}void Singleton::release(){ if (instance != NULL) { delete instance; instance = NULL; }}
main.cpp#include "Singleton.h"using namespace std;int main(int argc, const char * argv[]) { Singleton *s1 = Singleton::getInstance(); cout<<s1<<endl; Singleton *s2 = Singleton::getInstance(); cout<<s2<<endl; Singleton *s3 = Singleton::getInstance(); cout<<s3<<endl; s1->release(); s2->release(); s3->release(); return 0;}
饥饿式:
特点:一开始就创建了实例,所以每次用到的之后直接返回就好
C++代码:
Singleton.h#ifndef __C__Review__Singleton__#define __C__Review__Singleton__#include <iostream>class Singleton{ private: Singleton(){} Singleton(const Singleton& other){} Singleton& operator=(const Singleton& other); static Singleton *instance; public: static Singleton *getInstance();};#endif /* defined(__C__Review__Singleton__) */Singleton.m#include "Singleton.h"Singleton *Singleton::instance = new Singleton();Singleton* Singleton::getInstance(){ return instance;}
main.cpp#include "Singleton.h"using namespace std;int main(int argc, const char * argv[]) { Singleton *s1 = Singleton::getInstance(); cout<<s1<<endl; Singleton *s2 = Singleton::getInstance(); cout<<s2<<endl; Singleton *s3 = Singleton::getInstance(); cout<<s3<<endl; return 0;}
多线程式:
- class Lock
- {
- private:
- CCriticalSection m_cs;
- public:
- Lock(CCriticalSection cs) : m_cs(cs)
- {
- m_cs.Lock();
- }
- ~Lock()
- {
- m_cs.Unlock();
- }
- };
- class Singleton
- {
- private:
- Singleton();
- Singleton(const Singleton &);
- Singleton& operator = (const Singleton &);
- public:
- static Singleton *Instantialize();
- static Singleton *pInstance;
- static CCriticalSection cs;
- };
- Singleton* Singleton::pInstance = 0;
- Singleton* Singleton::Instantialize()
- {
- if(pInstance == NULL)
- { //double check
- Lock lock(cs); //用lock实现线程安全,用资源管理类,实现异常安全
- //使用资源管理类,在抛出异常的时候,资源管理类对象会被析构,析构总是发生的无论是因为异常抛出还是语句块结束。
- if(pInstance == NULL)
- {
- pInstance = new Singleton();
- }
- }
- return pInstance;
- }
0 0
- <c++>c++三种单例模式实现
- KMP字符串模式匹配的C实现
- Objective-C中单例模式的实现
- 【Objective-C】单例模式的实现
- Objective-C中单例模式的实现
- Objective-C中单例模式的实现
- Objective-C中单例模式的实现
- [C++] 单件模式的一般实现
- C语言实现SOLID原则的模式
- Object-C中单例模式的实现
- Objective-C 单例模式的实现
- Objective-C中单例模式的实现
- c++--Singleton单例模式的实现
- 设计模式的实现(C++)------工厂方法模式(FactoryMethod)
- 设计模式的实现(C++)------抽象工厂模式(AbstractFactory)
- 常见设计模式的解析和实现(C++)---Adapt模式
- 设计模式的解析和实现(C++)之一-Factory模式
- 设计模式的解析和实现(C++)之一-Factory模式
- Codeforces Round #318 (Div. 2) D - Bear and Blocks
- wxWidgets和QT之间的选择
- 链表翻转的递归和非递归实现
- ZOJ 2112 Dynamic Rankings 树状数组套主席树 单点修改求动态区间第K大
- 读书笔记之深入理解操作系统(10)
- 三种单例模式的C++实现
- CodeForces 337A - Puzzles
- redis的学习地址
- eclipse 自动生成注释
- C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配
- SQLite3 写数据库时的锁机制
- Makefile经典教程(掌握这些足够)
- html页面
- TCP/IP 协议 建立与链接