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

单例模式

在这里插入图片描述

C++

饿汉

/* 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;
}

懒汉

/* 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

饿汉

/* 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;
}

懒汉

/* 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;
}

相关推荐

  1. 设计模式

    2024-07-10 00:14:06       59 阅读
  2. 设计模式

    2024-07-10 00:14:06       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 00:14:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 00:14:06       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 00:14:06       58 阅读
  4. Python语言-面向对象

    2024-07-10 00:14:06       69 阅读

热门阅读

  1. Python面试题:如何在 Python 中实现单例模式?

    2024-07-10 00:14:06       25 阅读
  2. react动态渲染列表与函数式组件

    2024-07-10 00:14:06       21 阅读
  3. 垃圾回收器详解

    2024-07-10 00:14:06       19 阅读
  4. homebrew常用命令

    2024-07-10 00:14:06       21 阅读
  5. JVM详解

    JVM详解

    2024-07-10 00:14:06      19 阅读
  6. 学习.NET 8 MiniApis入门

    2024-07-10 00:14:06       17 阅读
  7. Windows使用 Gitee+PicGo 搭建Markdown图床

    2024-07-10 00:14:06       21 阅读
  8. Codeforces Round 925 (Div. 3) D-F

    2024-07-10 00:14:06       25 阅读
  9. 2024华为OD机试真题-找数字-(C++/Python)-C卷D卷-200分

    2024-07-10 00:14:06       21 阅读
  10. 深入理解基本数据结构:数组详解

    2024-07-10 00:14:06       26 阅读
  11. py每日spider案例之magnet篇

    2024-07-10 00:14:06       21 阅读
  12. 面向对象编程在Perl中的实现:解锁Perl的OOP潜力

    2024-07-10 00:14:06       23 阅读