设计模式: 单例模式
创始人
2024-05-30 18:24:59
0

目录

  • 单例模式
  • 应用场景
  • 实现步骤
  • 涉及知识点
  • 设计与实现

单例模式

通过单例模式的方法创建的类在当前进程中只有一个实例;

应用场景

配置管理
日志记录
线程池
连接池
内存池
对象池
消息队列

实现步骤

将类的构造方法定义为私有方法
定义一个私有的静态实例
提供一个公有的获取实例的静态方法

涉及知识点

static静态成员数据
static静态成员函数
template模板类
friend友元类

设计与实现

常用实现
模板实现
线程安全

main.c

#include"Singleton.h"
using namespace mySingleTon;
void test() {//A::instance()->show();//B::instance()->show();singleTon::instance()->show();singleTon::instance()->show();
}

A.h

//#pragma once 
//#include
//#include
//using namespace std;
//
//class A {
//
//public:
//    static A* instance() {
//        if (m_instance == nullptr) {
//            m_instance = new A();
//        }
//        return m_instance;
//    }
//    void show() {
//        cout << mName << endl;
//    }
//private:
//    A():mName("A"){}
//    A(const A&);
//    ~A();
//    A & operator=(const A&);
//private:
//    static A* m_instance;
//    string mName;
//};
//A* A::m_instance = nullptr;
#pragma once 
#include
#include
#include"Singleton.h"
using namespace std;class A {friend class mySingleTon::singleTon;
public:void show() {cout << mName << endl;}
private:A():mName("A"){}A(const A&);~A();A & operator=(const A&);
private:static A* m_instance;string mName;
};
A* A::m_instance = nullptr;

B.h

/*
#pragma once
#include
#include
#include"Singleton.h"
using namespace std;class B {friend class mySingleTon::singleTon;
public:static B* instance() {if (m_instance == nullptr) {m_instance = new A();}return m_instance;}void show() {cout << mName << endl;}
private:B() :mName("B") {}B(const B&);~B();B& operator=(const B&);
private:static B* m_instance;string mName;
};
B* B::m_instance = nullptr;
*/
#pragma once
#include
#include
#include"Singleton.h"
using namespace std;class B {friend class mySingleTon::singleTon;
public:void show() {cout << mName << endl;}
private:B() :mName("B") {}B(const B&);~B();B& operator=(const B&);
private:static B* m_instance;string mName;
};
B* B::m_instance = nullptr;

懒汉式
多线程时,是线程不安全的
Singleton.h

#pragma once
namespace mySingleTon {
template
class singleTon {
public:static T* instance() {if (m_instance == nullptr) {m_instance = new T();}return m_instance;}private:singleTon() {};singleTon(const singleTon&){}~singleTon(){}static T* m_instance;singleTon& operator =(const singleTon);};
template
T* singleTon::m_instance = nullptr;};

饿汉式
多线程时,是线程安全的
Singleton.h

#pragma once
namespace mySingleTon {
template
class singleTon {
public:static T* instance() {if (m_instance == nullptr) {m_instance = new T();}return m_instance;}private:singleTon() {};singleTon(const singleTon&){}~singleTon(){}static T* m_instance;singleTon& operator =(const singleTon);};
template
T* singleTon::m_instance = new singleTon ;
};

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...