单例模式(大话设计模式)C/C++版本

单例模式

C++

饿汉

cpp 复制代码
/* HM hungry man 饿汉 */
#include <iostream>
using namespace std;
class Singleton
{
private:
    Singleton() { cout << "单例对象创建!" << endl; };
    Singleton(const Singleton &);
    Singleton &operator=(const Singleton &);
    ~Singleton() { cout << "单例对象销毁!" << endl; };

    static Singleton *myInstance;

public:
    static Singleton *getInstance()
    {
        return myInstance;
    }
    static void releaseInstance()
    {
        delete myInstance;
    }
};

Singleton *Singleton::myInstance = new Singleton();
int main()
{
    Singleton *ct1 = Singleton::getInstance();
    Singleton *ct2 = Singleton::getInstance();
    Singleton *ct3 = Singleton::getInstance();
    Singleton::releaseInstance();
    return 0;
}

懒汉

cpp 复制代码
/* LM lazy man 懒汉 */
#include <iostream>
using namespace std;
class Singleton
{
private:
    Singleton() { cout << "单例对象创建!" << endl; };
    Singleton(const Singleton &);
    Singleton &operator=(const Singleton &);
    ~Singleton() { cout << "单例对象销毁!" << endl; };

public:
    static Singleton *getInstance()
    {
        static Singleton myInstance;
        return &myInstance;
    }
};

int main()
{
    Singleton *ct1 = Singleton::getInstance();
    Singleton *ct2 = Singleton::getInstance();
    Singleton *ct3 = Singleton::getInstance();
    return 0;
}

C

饿汉

c 复制代码
/* HM hungry man 饿汉 */
#include <stdio.h>

// 定义单例结构体
typedef struct
{
    int initialized;
} Singleton;

// 静态全局变量,初始化单例对象
static Singleton singleton = {1};

// 单例对象的访问函数
Singleton *getSingleton(void)
{
    static Singleton *ptr = &singleton; // 静态局部变量,初始化单例对象指针
    return ptr;
}

int main()
{
    Singleton *ct1 = getSingleton();
    Singleton *ct2 = getSingleton();
    Singleton *ct3 = getSingleton();

    // 注意:在C语言中,单例对象的生命周期通常与程序相同,
    // 不需要显式释放,因为程序结束时资源会自动释放。
    // 但是,如果单例对象持有需要释放的资源,应该在适当的地方释放。

    return 0;
}

懒汉

c 复制代码
/* LM lazy man 懒汉 */
#include <stdio.h>

typedef struct
{
    int initialized;
} Singleton;

static Singleton *getSingleton(void)
{
    static Singleton instance = {0}; // 静态局部变量,只初始化一次
    static Singleton *ptr = NULL;    // 静态局部指针,用于返回单例对象

    if (ptr == NULL)
    {
        ptr = &instance;          // 第一次调用时初始化指针
        instance.initialized = 1; // 标记单例对象已初始化
        printf("单例对象创建!\n");
    }

    return ptr;
}

int main()
{
    Singleton *ct1 = getSingleton();
    Singleton *ct2 = getSingleton();
    Singleton *ct3 = getSingleton();

    // 如果需要显示单例对象的生命周期结束,可以在这里释放资源或做一些清理工作
    // 但请注意,C语言中单例对象的销毁通常是在程序结束时自动发生的

    return 0;
}
相关推荐
lilili也39 分钟前
visual studio add matlab
c语言·c++
阿米亚波6 小时前
【C/C++包管理器】vcpkg(by microsoft)
c语言·c++·git·vscode·microsoft·github·vcpkg
SNAKEpc121386 小时前
OpenGL(十一)- 变换管线
c语言·c++·算法·矩阵·图形渲染
小小龙学IT6 小时前
Day 26-27 项目实战:从零构建一个高并发聊天室(epoll + 线程池)
linux·服务器·c语言·开发语言·网络
c238567 小时前
MySQL 基础用法(下):查询进阶与核心特性
android·c语言·c++·mysql
十月的皮皮8 小时前
STM32从零到量产开发:四路继电器工业控制模块开发 - 上位机操作说明书
c语言·stm32·单片机·stm32cubemx
小僧景贤9 小时前
嵌入式C语言 第十一篇:成长路线|嵌入式C工程师完整进阶学习路径(从零到工程级落地|含周期+实战项目)
c语言·开发语言·学习
灵晔君9 小时前
【Linux】进程(四)——进程虚拟地址空间
linux·c语言·开发语言
wengqidaifeng9 小时前
2026 年电赛(TI 杯)控制类 H 题:8 路灰度循迹、直线/半圆分段控制与 S1/S2/S3 状态机
c语言·单片机·嵌入式硬件
天空'之城10 小时前
C 语言工业级通用组件手写 25:简易日志系统
c语言·开发语言·日志系统·嵌入式调试·调试打印