单例模式学习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() 函数:展示了如何获取和使用这些单例实例,并验证每个类只能有一个实例。
相关推荐
2303_Alpha2 天前
SpringBoot
笔记·学习
萘柰奈2 天前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
沐矢羽2 天前
Tomcat PUT方法任意写文件漏洞学习
学习·tomcat
好奇龙猫2 天前
日语学习-日语知识点小记-进阶-JLPT-N1阶段蓝宝书,共120语法(10):91-100语法+考え方13
学习
向阳花开_miemie2 天前
Android音频学习(十八)——混音流程
学习·音视频
工大一只猿2 天前
51单片机学习
嵌入式硬件·学习·51单片机
c0d1ng2 天前
量子计算学习(第十四周周报)
学习·量子计算
Hello_Embed3 天前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中3 天前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Magnetic_h3 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa