C++单例模式

在C++中,单例模式是一种确保一个类只有一个实例,并提供一个全局访问点来访问这个实例的设计模式。

单例模式中的饿汉式(Eager Initialization)和懒汉式(Lazy Initialization)是两种不同的实例化策略,它们在实例的创建时机、性能和线程安全性方面有所区别。

以下是一个单例模式的典型实现:

饿汉式(Eager Initialization)

  1. 实例创建时机

    • 在程序启动时或类加载时立即创建单例实例。
  2. 性能

    • 由于实例在程序开始时就已经创建,因此第一次调用 getInstance 方法时不需要进行任何同步操作,速度较快。
  3. 线程安全性

    • 饿汉式是线程安全的,因为实例在类加载时就已经创建,JVM(Java虚拟机)保证了类加载过程的线程安全性。
  4. 资源使用

    • 如果单例实例很大或者初始化过程很耗时,并且程序可能永远不使用这个实例,那么饿汉式可能会浪费资源。
  5. 实现

    • 通常通过静态常量实现。

在这种方法中,单例实例在程序开始时就被创建。

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

private:
    Singleton() {}
    static Singleton instance; // 静态常量
};

Singleton Singleton::instance; // 在程序启动时创建实例

懒汉式(Lazy Initialization)

  1. 实例创建时机

    • 在第一次调用 getInstance 方法时创建单例实例。
  2. 性能

    • 第一次调用 getInstance 时需要创建实例,可能会有一定的延迟,但如果实例很大或者初始化很耗时,那么这种方式可以延迟资源的使用,提高性能。
  3. 线程安全性

    • 基本的懒汉式实现不是线程安全的,因为在多线程环境中可能会有多个线程同时进入 if 判断并创建多个实例。
    • 为了保证线程安全,通常需要添加同步机制(如互斥锁),但这会降低性能。
  4. 资源使用

    • 懒汉式只在需要时创建实例,更加节省资源。
  5. 实现

    • 通常通过静态成员变量和同步代码块实现。
class Singleton {
public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }

private:
    Singleton() {}
    static Singleton* instance; // 静态成员变量
};

Singleton* Singleton::instance = nullptr; // 初始时为空

 为了使懒汉式线程安全,可以添加互斥锁:

#include <mutex>

class Singleton {
public:
    static Singleton* getInstance() {
        if (instance == nullptr) {
            std::lock_guard<std::mutex> lock(mutex_);
            if (instance == nullptr) {
                instance = new Singleton();
            }
        }
        return instance;
    }

private:
    Singleton() {}
    static Singleton* instance; // 静态成员变量
    static std::mutex mutex_;  // 互斥锁
};

Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mutex_;

懒汉式使用示例

Student* Student::instance_ = NULL;
Student Student::staticInstance;

class Student: public people
{
public:
    static Student * instance(){return instance_;};

private:

    Student ();
    virtual ~Student ();

    static Student * instance_;
    static Student staticInstance;

    Student (const Student &);
    Student & operator = (const Student &);
};
Student ::Student ():
{
    instance_ = &staticInstance;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值