单例模式(懒汉模式/饿汉模式)

相关概念参考:【C++】C++ 单例模式总结(5种单例实现方法)_单例模式c++实现-优快云博客

#include<iostream>

class LazySingle{
public:
    static LazySingle& getInstance(){
        static LazySingle instance;
        return instance;
    }

    void hello(){
        std::cout<<"Congratulations"<<std::endl;
    }

private:
    LazySingle(){};
};
class LazySinglePtr{
    public:
        static LazySinglePtr* getInstance(){
            static LazySinglePtr instance;
            return &instance;
        }
    
        void hello(){
            std::cout<<"Congratulations"<<std::endl;
        }
    
    private:
        LazySinglePtr(){};
};

class HungrySingle{
public:
    static HungrySingle& getInstance(){
        return instance_;
    }

private:
    static HungrySingle instance_;
    HungrySingle(){};
};

HungrySingle  HungrySingle::instance_;

int main(){
    std::cout<<"懒汉引用模式:"<<std::endl;
    auto& lazy1=LazySingle::getInstance();
    LazySingle& lazy2=LazySingle::getInstance();
    std::cout<<" lzay1="<<&lazy1<<" lzay2="<<&lazy2<<" instance="<<&LazySingle::getInstance()<<std::endl;
    lazy1.hello();

    std::cout<<"懒汉指针模式:"<<std::endl;
    auto lazyptr1=LazySinglePtr::getInstance();
    LazySinglePtr* lazyptr2=LazySinglePtr::getInstance();
    std::cout<<" lzay1="<<lazyptr1<<" lzay2="<<lazyptr2<<" instance="<<LazySinglePtr::getInstance()<<std::endl;
    lazyptr1->hello();
    
    std::cout<<"饿汉模式:"<<std::endl;
    HungrySingle& hungry1=HungrySingle::getInstance();
    HungrySingle& hungry2=HungrySingle::getInstance();
    std::cout<<" hungry1="<<&hungry1<<" hungry2="<<&hungry2<<" instance="<<&HungrySingle::getInstance()<<std::endl;

    return 0;
    
}

输出:

懒汉引用模式:
 lzay1=0x7ff61a279e90 lzay2=0x7ff61a279e90 instance=0x7ff61a279e90
Congratulations
懒汉指针模式:
 lzay1=0x7ff61a279ee0 lzay2=0x7ff61a279ee0 instance=0x7ff61a279ee0
Congratulations
饿汉模式:
 hungry1=0x7ff61a2aa030 hungry2=0x7ff61a2aa030 instance=0x7ff61a2aa030

 

单例模式是一种常用的设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。懒汉模式饿汉模式是实现单例模式的两种主要方式,它们在实例化时机、线程安全性和性能方面有显著区别。 ### 实现方式 **饿汉模式**在类加载时就完成了实例的创建,无论是否使用该实例。这种实现方式简单且线程安全,因为实例化过程发生在类加载阶段,不需要额外的同步措施。例如: ```cpp class Singleton1 { public: static Singleton1* getInstance() { return instance; } protected: Singleton1() { // 构造函数 } private: static Singleton1* instance; }; Singleton1* Singleton1::instance = new Singleton1(); ``` **懒汉模式**则是在第一次调用 `getInstance` 方法时才创建实例,这种方式可以延迟加载,节省资源,但需要处理多线程环境下的同步问题。常见的实现如下: ```java public class Singleton2 { private Singleton2() { } private static Singleton2 instance; public static Singleton2 getInstance() { if (instance == null) { instance = new Singleton2(); } return instance; } } ``` ### 区别 **线程安全性**:饿汉模式由于在类加载时就创建了实例,因此天然地具有线程安全性[^1]。懒汉模式则需要通过同步机制来保证线程安全,例如使用 `synchronized` 关键字修饰 `getInstance` 方法,但这可能会导致性能下降。 **性能**:饿汉模式可能会导致资源浪费,因为它在类加载时就创建了实例,即使这个实例可能永远不会被用到。懒汉模式避免了这个问题,因为它只在需要时才创建实例,但首次调用 `getInstance` 方法时可能会有轻微的性能开销。 **资源利用**:懒汉模式能够更好地利用资源,特别是在实例创建成本较高或占用大量内存的情况下。而饿汉模式则可能在程序启动时就消耗较多资源。 ### 选择依据 选择懒汉模式还是饿汉模式取决于具体的应用场景。如果应用程序对启动性能有较高要求,并且实例的创建成本不高,可以选择懒汉模式以节省资源。反之,如果希望确保线程安全且不介意启动时的资源消耗,可以选择饿汉模式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值