单例模式学习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() 函数:展示了如何获取和使用这些单例实例,并验证每个类只能有一个实例。
相关推荐
hsjiasb4 小时前
FreeRTOS学习(三十九)——最终总结
学习·学习笔记·freertos
m4Rk_5 小时前
【论文阅读】Agent 记忆机制(47):Nemori——用“预测误差”判断什么经验值得被记住
论文阅读·人工智能·学习·开源·github
约克36 小时前
2024年空调除甲醛技术参数与性能评测解析
学习
kkkkkkkkkk_Z6 小时前
学嵌入式和C语言编程数据结构|学习日记Day21:内核链表与队列学习
c语言·数据结构·学习
kdxiaojie8 小时前
Linux USB驱动阅读笔记(1)
linux·笔记·学习·usb
呼噜想睡觉8 小时前
Bootstrap3完成从导航到页脚完整页面开发
学习
深蓝海拓11 小时前
基于QtPy (PySide6) 的PLC-HMI工程项目(十五)后续完善和改进:PLC端数据解析的防粘包、残包、废包、拆包
笔记·学习·plc
小黄蚁11 小时前
LVGL学习笔记(六)
笔记·学习
zyf10441612 小时前
暑期实践日志 Day33:整体复盘核查视频,完善剪辑成果
学习·计算机网络·剪辑·暑期实践·课题任务
01_ice12 小时前
前端学习css
前端·css·学习