单例模式学习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() 函数:展示了如何获取和使用这些单例实例,并验证每个类只能有一个实例。
相关推荐
非凡ghost1 小时前
CoolUtils PDF Combine(PDF合并工具)
windows·学习·pdf·软件需求
阿W呀1 小时前
【光的偏振与光功率 / 能量测量学习笔记】
学习
allan bull2 小时前
在节日中寻找平衡:圣诞的欢乐与传统节日的温情
人工智能·学习·算法·职场和发展·生活·求职招聘·节日
wdfk_prog2 小时前
[Linux]学习笔记系列 -- [fs]fs-writeback
linux·笔记·学习
charlie1145141912 小时前
嵌入式现代C++教程:C++98——从C向C++的演化(3)
c语言·开发语言·c++·笔记·学习·嵌入式
RanceGru2 小时前
LLM学习笔记8——多模态CLIP、ViLT、ALBEF、VLMo、BLIP
笔记·学习
华舞灵瞳3 小时前
学习FPGA(七)正弦信号合成
学习·fpga开发
im_AMBER3 小时前
weather-app开发手记 04 AntDesign组件库使用解析 | 项目设计困惑
开发语言·前端·javascript·笔记·学习·react.js
好奇龙猫5 小时前
【AI学习-comfyUI学习-第二十三-法线贴图工作流-depth 结构+MiDaS 法线-各个部分学习】
人工智能·学习·贴图
Nan_Shu_6145 小时前
学习:Java (1)
java·开发语言·学习