当前位置: 首页 > news >正文

单例模式学习cpp

现在我们要求定义一个表示总统的类型。presented可以从该类型继承出French present和American present的等类型。这些派生类型都只能产生一个实例

为了设计一个表示总统的类型,并从该类型派生出只能产生一个实例的具体总统(如法国总统和美国总统),我们可以利用单例模式和继承来实现。下面是一个可能的设计方案:

  1. 定义基类 President:该基类可以包含一些共有的属性和方法。
  2. 派生具体总统类 FrenchPresidentAmericanPresident:每个派生类都实现单例模式,确保只能有一个实例。

下面是具体的代码实现:

#include <iostream>
#include <memory>
#include <mutex>// 基类 President
class President {
public:virtual void showIdentity() const = 0;protected:President() {}virtual ~President() {}
};// 法国总统 FrenchPresident 类
class FrenchPresident : public President {
public:static FrenchPresident& getInstance() {static FrenchPresident instance;return instance;}void showIdentity() const override {std::cout << "I am the French President." << std::endl;}// 删除拷贝构造函数和赋值运算符FrenchPresident(const FrenchPresident&) = delete;FrenchPresident& operator=(const FrenchPresident&) = delete;private:FrenchPresident() {}~FrenchPresident() {}
};// 美国总统 AmericanPresident 类
class AmericanPresident : public President {
public:static AmericanPresident& getInstance() {static AmericanPresident instance;return instance;}void showIdentity() const override {std::cout << "I am the American President." << std::endl;}// 删除拷贝构造函数和赋值运算符AmericanPresident(const AmericanPresident&) = delete;AmericanPresident& operator=(const AmericanPresident&) = delete;private:AmericanPresident() {}~AmericanPresident() {}
};int main() {// 获取法国总统实例FrenchPresident& frenchPresident = FrenchPresident::getInstance();frenchPresident.showIdentity();// 获取美国总统实例AmericanPresident& americanPresident = AmericanPresident::getInstance();americanPresident.showIdentity();// 确保每个类只能有一个实例FrenchPresident& frenchPresident2 = FrenchPresident::getInstance();AmericanPresident& americanPresident2 = AmericanPresident::getInstance();if (&frenchPresident == &frenchPresident2) {std::cout << "Both FrenchPresident instances are the same." << std::endl;}if (&americanPresident == &americanPresident2) {std::cout << "Both AmericanPresident instances are the same." << std::endl;}return 0;
}

解释

  1. 基类 President:定义了一个纯虚函数 showIdentity(),使得派生类必须实现该方法。
  2. FrenchPresidentAmericanPresident
    • 实现了单例模式,通过 getInstance() 方法返回类的唯一实例。
    • 私有化了构造函数、拷贝构造函数和赋值运算符,以确保无法从外部创建实例或拷贝实例。
    • 实现了基类的纯虚函数 showIdentity(),提供了具体的身份信息。
  3. main() 函数:展示了如何获取和使用这些单例实例,并验证每个类只能有一个实例。
http://www.lryc.cn/news/405511.html

相关文章:

  • 第5讲:Sysmac Studio中的硬件拓扑
  • 使用GoAccess进行Web日志可视化
  • GD 32 流水灯
  • 数据结构之栈详解
  • 算法:BFS解决 FloodFill 算法
  • Python 中文双引号 “”
  • 以太网(Ethernet)
  • Scrcpy adb server version (41) doesn‘t match this client (39); killing...
  • 微服务实战系列之玩转Docker(四)
  • 微信小程序-自定义组件生命周期
  • 2024年7月23日(samba DNS)
  • Hyperledger顶级项目特点和介绍
  • 操作系统——笔记(1)
  • isEmpty() 和 isBlank()的区别
  • scrapy生成爬虫数据为excel
  • vscode debug C++无法输入问题
  • MODBUS tcp学习总结
  • 【第一天】计算机网络 TCP/IP模型和OSI模型,从输入URL到页面显示发生了什么
  • 发现FionaAI:免费体验最新的GPT-4o Mini模型!
  • Linux Gui 窗口对话和窗口操作
  • 人工智能驾驶技术:引领未来道路
  • 管理的核心是管人,管人的核心就是这3条,看懂的是高手
  • 代码解读:Diffusion Models中的长宽桶技术(Aspect Ratio Bucketing)
  • Linux下如何使用GitLab进行团队协作
  • 无法连接到internet怎么办?已连接但无internet访问,其实并不难
  • 建投数据人力资源系列产品获得欧拉操作系统及华为鲲鹏技术认证书
  • 【iOS】——属性关键字的底层原理
  • 电影类平台如何选择服务器
  • 递归神经网络(RNN)及其预测和分类的Python和MATLAB实现
  • 以flask为后端的博客项目——星云小窝