C++设计模式-更新中

单例模式

这个类实现了单例模式。单例模式是一种设计模式,旨在确保一个类只有一个实例,并提供一个全局访问点来获取该实例。

ConnectionManager 类中,它通过以下方式实现了单例模式:

  1. 构造函数 ConnectionManager() 被声明为私有外部无法直接创建类的实例。
  2. 通过静态成员函数 getInstance() 来获取 ConnectionManager 类的实例。该函数使用了一个静态的 ConnectionManager 类型对象,并在首次调用时创建该对象,并保证只有一个实例被创建。
  3. 复制构造函数和赋值运算符被删除,禁止直接进行对象的拷贝构造和赋值操作,确保只有一个实例存在。

通过实现这些特性,ConnectionManager 类确保只能通过单例实例来访问和操作类的方法和成员变量,从而实现了单例模式。

cpp 复制代码
#ifndef RDMA_SERVER_CONNECTIONMANAGER_H
#define RDMA_SERVER_CONNECTIONMANAGER_H

#pragma once

#include "../common/common.h"
#include "../queue_manager/QueueManager.h"
#include "../buffer_manager/BufferManager.h"
#include "./Connection.h"
// 是的,这个类实现了单例模式。单例模式是一种设计模式,旨在确保一个类只有一个实例,并提供一个全局访问点来获取该实例。

// 在 `ConnectionManager` 类中,它通过以下方式实现了单例模式:

// 1. 构造函数 `ConnectionManager()` 被声明为私有,外部无法直接创建类的实例。
// 2. 通过静态成员函数 `getInstance()` 来获取 `ConnectionManager` 类的实例。该函数使用了一个静态的 `ConnectionManager` 类型对象,
//并在首次调用时创建该对象,并保证只有一个实例被创建。
// 3. 复制构造函数和赋值运算符被删除,禁止直接进行对象的拷贝构造和赋值操作,确保只有一个实例存在。

// 通过实现这些特性,`ConnectionManager` 类确保只能通过单例实例来访问和操作类的方法和成员变量,从而实现了单例模式。
class ConnectionManager {
public:
//单例模式
    static ConnectionManager &getInstance();

    ConnectionManager(ConnectionManager const &) = delete;

    void operator=(ConnectionManager const &) = delete;

    ~ConnectionManager();

    Connection *create(struct ibv_device *);

    void destroy(Connection *);

    struct ibv_qp *createQueuePair(Connection *, struct ibv_qp_attr *);

    void connectQueuePair(Connection *, uint32_t, uint32_t, ibv_mtu, struct ibv_qp_attr *);

    void resetQueuePair(struct ibv_qp *, struct ibv_qp_attr *);

private:
    ConnectionManager();

    std::shared_ptr<spdlog::logger> logger = nullptr;

    std::shared_ptr<QueueManager> queueManager = std::make_shared<QueueManager>();

    std::mutex queuePairsMtx;
};

#endif
相关推荐
优雅的潮叭3 分钟前
c++ 学习笔记之 shared_ptr
c++·笔记·学习
SunkingYang11 分钟前
QT中使用Lambda表达式作为槽函数用法,以及捕获列表和参数列表用法与区别
c++·qt·用法·lambda表达式·捕获列表·槽函数·参数列表
微露清风32 分钟前
系统性学习C++-第二十二讲-C++11
java·c++·学习
qq_3168377535 分钟前
uni.chooseMedia 读取base64 或 二进制
开发语言·前端·javascript
gis开发1 小时前
【无标题】
java·前端·javascript
代码村新手2 小时前
C++-类和对象(中)
java·开发语言·c++
梦因you而美2 小时前
XPath 元素定位全方位技术文档
javascript·xpath·xpath定位
天意pt2 小时前
Idempotency 幂等性 - 点赞和投票功能
前端·javascript·express
Ccjf酷儿3 小时前
C++语言程序设计 (郑莉)第十章 泛型程序设计与C++标准模板库
开发语言·c++
FreeBuf_3 小时前
利用零宽度字符的隐形JavaScript混淆工具InvisibleJS浮出水面
开发语言·javascript·ecmascript