悉野小楼

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

C++ 单例模式三种写法

复制代码
#include <iostream>
#include "Apple.h"

#include "Singleton.h"
#include "ActivityManager.h"
#include "ResourceManager.h"
using namespace MySpace;

int main()
{
    Apple::abc = 10;
    //参考:https://blog.csdn.net/unonoi/article/details/121138176
    // 
    /*
    * 单例模式可以分为 懒汉式 和 饿汉式 ,两者之间的区别在于创建实例的时间不同。

    懒汉式
    系统运行中,实例并不存在,只有当需要使用该实例时,才会去创建并使用实例。这种方式要考虑线程安全。

    饿汉式
    系统一运行,就初始化创建实例,当需要时,直接调用即可。这种方式本身就线程安全,没有多线程的线程安全问题。
    */

    //方法一: 饿汉式单例(本身就线程安全), 在系统启动时就创建好了
    Apple& app = Apple::Instance();
    app.SayHello();
    //用宏方便写
    gApple.SayHello();


    //方法二:
    MySpace::ResourceManager::Instance().SayHello();

    
    //方法三: 使用一个单例宏
    MySpace::Singleton<ActivityManager>::instance().SayHello();
    ACTIVITY_MANAGER.SayHello();
    system("pause");
}
复制代码

Apple.h

复制代码
#pragma once
#include <iostream>
class Apple
{
public:
    static Apple& Instance() { return s_kInstance; }
    static int abc;
public:
    inline void SayHello() { std::cout << "Apple Hello" << std::endl; }
private:
    //Apple(); 没写, 这样CPP就不用写了
    //~Apple();

    Apple()
    {
        std::cout << "Apple Constructor" << std::endl;
    }
    static Apple s_kInstance;
};

#define gApple (Apple::Instance())
复制代码

Apple.cpp

#include "Apple.h"
Apple Apple::s_kInstance;
int Apple::abc = 5;

ResourceManager.h

复制代码
#pragma once
#include <iostream>

namespace MySpace
{
    class ResourceManager
    {
    public:
        static ResourceManager& Instance();
        void SayHello();
        ResourceManager()
        {
            std::cout << "ResourceManager Constructor" << std::endl;
        }
    };
}
复制代码

 

ResourceManager.cpp

复制代码
#include "ResourceManager.h"

using namespace MySpace;

ResourceManager& ResourceManager::Instance()
{
    static ResourceManager resMgr;
    return resMgr;
}
void ResourceManager::SayHello()
{
    std::cout << "ResourceManager Hello" << std::endl;
}
复制代码

 

Singleton.h

复制代码
#pragma once

namespace MySpace
{
    template<typename T>
    class Singleton
    {
    public:
        static T& instance()
        {
            static T t;
            return t;
        }

    private:
        Singleton();
        ~Singleton();
        Singleton(const Singleton&);
        Singleton& operator=(const Singleton&);
    };
}
复制代码

 

ActivityManager.h

复制代码
#pragma once
#include <iostream>

namespace MySpace
{
    class ActivityManager
    {
    public:
        void SayHello();
        ActivityManager()
        {
            std::cout << "ActivityManager Constructor" << std::endl;
        }
    };
    #define ACTIVITY_MANAGER MySpace::Singleton<ActivityManager>::instance()
}
复制代码

 

ActivityManager.cpp

#include "ActivityManager.h"

using namespace MySpace;

void ActivityManager::SayHello()
{
    std::cout << "ActivityManager Hello" << std::endl;
}

 

下载

posted on   悉野  阅读(232)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2013-07-25 MFC学习 文件操作注册表操作
点击右上角即可分享
微信分享提示