c++ 单例模式简介和应用场景

1. 单例模式定义

设计一个类,我们只能生产该类的一个实例,并提供一个访问它的全局访问点

2.单例模式存在的意义

更新一下自己对于设计模式的理解。每个设计模式看似很简单,实则想要在一个完整的系统中应用还是非常非常难的。

  1. 一个class只有一个对象,减少了内存开支 ,对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级对象而言,是非常可观的一笔系统开销
  2. 避免对资源多重占用,由于 new 操作的次数减少,因而对系统内存的使用频率也会降低,这将减轻 GC 压力,缩短 GC 停顿时间;

3.适用于单例模式的场景

  1. 频繁创建及销毁的对象,例如工具类
  2. 不变的对象
  3. 重量级对象,例如JDBC连接,httpClient等
  4. 线程池,缓存
  5. 打印机,日志对象,

4.单例模式的使用

4.1饿汉模式(线程安全)

/**
  * 恶汉式
  * 优点:类加载时就完成了实例化,避免了线程同步问题。
  * 缺点:static一直强引用这对象,直至类被销毁对象才会被销毁。可能造成内存浪费
  */
public class Singleton {
	//在静态初始化器中创建单例实例,这段代码保证了线程安全
    private static Singleton INSTANCE = new Singleton();
//Singleton类只有一个构造方法并且是被private修饰的,所以用户无法通过new方法创建该对象实例
    private Singleton(){}

    public static Singleton getInstance(){
        return INSTANCE;
    }
}

饿了肯定要饥不择食。所以在单例类定义的时候就进行实例化,这里使用的了static 所以代码在编译的时候这个类时就马上创建此唯一的单例实例,不管你用不用,先创建了再说,如果一直没有被使用,便浪费了空间,典型的空间换时间,每次调用的时候,就不需要再判断,节省了运行时间。
由于要进行线程同步,所以在访问量比较大,或者可能访问的线程比较多时,采用饿汉实现,可以实现更好的性能。这是以空间换时间

4.2懒汉模式(线程安全)

1.加锁实现线程安全的懒汉模式

class Singleton 
{ 
  public: 
    static pthread_mutex_t mutex; 
    static Singleton* getInstance(); 
  protected: 
    Singleton() 
    { 
      pthread_mutex_init(&mutex); 
    } 
  private: 
    static Singleton* p; 
}; 

pthread_mutex_t Singleton::mutex; 
Singleton* Singleton::p = NULL; 
Singleton* Singleton::getInstance() 
{ 
  if (NULL == p) 
  { 
    pthread_mutex_lock(&mutex); 
    if (NULL == p) 
      p = new Singleton(); 
    pthread_mutex_unlock(&mutex); 
  } 
  return p; 
}

2.内部静态变量实现懒汉模式

class Singleton 
{ 
  public: 
  static pthread_mutex_t mutex; 
  static Singleton* getInstance(); 
  protected: 
    Singleton() 
    { 
      pthread_mutex_init(&mutex); 
    } 
}; 

pthread_mutex_t Singleton::mutex; 
Singleton* Singleton::getInstance() 
{ 
  pthread_mutex_lock(&mutex); 
  static singleton ss; 
  pthread_mutex_unlock(&mutex); 
  return &ss; 
}

懒汉:故名思义,不到万不得已就不会去实例化类,也就是说在第一次用到类实例的时候才会去实例化,所以上边的经典方法被归为懒汉实现;
在访问量较小时,采用懒汉实现。这是以时间换空间。

参考文档]

1 https://www.cnblogs.com/developing/articles/10891234.html
2 https://blog.youkuaiyun.com/qq_34337272/article/details/80455972

### C++单例模式的适用场景 单例模式是一种创建型设计模式,其核心目标是确保某个类仅有一个实例,并提供一个全局访问点[^2]。以下是单例模式C++ 中的一些典型适用场景: #### 1. **资源管理** 当应用程序需要统一管理分配某种有限资源时,可以使用单例模式来确保只有一个管理者负责这些操作。例如,在多线程环境中管理线程池或数据库连接池。 ```cpp class ThreadManager { private: static ThreadManager* instance; ThreadManager() {} // 私有构造函数防止外部实例化 public: static ThreadManager& GetInstance() { if (!instance) { instance = new ThreadManager(); } return *instance; } void ManageThreads() { std::cout << "Managing threads..." << std::endl; } }; ThreadManager* ThreadManager::instance = nullptr; int main() { ThreadManager::GetInstance().ManageThreads(); } ``` 上述代码展示了如何通过单例模式管理线程池或其他共享资源[^4]。 --- #### 2. **日志记录器** 在一个应用中,通常希望所有的模块都向同一个日志文件写入数据。此时可以通过单例模式实现一个全局的日志记录器对象。 ```cpp #include <iostream> #include <fstream> class Logger { private: static Logger* instance; std::ofstream logFile; Logger(const std::string& filename) : logFile(filename, std::ios::app) {} public: static Logger& GetInstance(const std::string& filename = "log.txt") { if (!instance) { instance = new Logger(filename); } return *instance; } void Log(const std::string& message) { logFile << message << std::endl; } }; Logger* Logger::instance = nullptr; int main() { Logger::GetInstance().Log("Application started."); Logger::GetInstance().Log("An error occurred."); } ``` 此示例说明了单例模式适用于集中式日志记录的需求[^3]。 --- #### 3. **配置管理** 许多应用程序需要读取解析配置文件,并在整个程序生命周期内保持一致的状态。单例模式非常适合用来存储分发这些配置信息。 ```cpp #include <unordered_map> #include <string> class ConfigManager { private: static ConfigManager* instance; std::unordered_map<std::string, std::string> configMap; ConfigManager() {} public: static ConfigManager& GetInstance() { if (!instance) { instance = new ConfigManager(); } return *instance; } void LoadConfig(const std::string& key, const std::string& value) { configMap[key] = value; } std::string GetValue(const std::string& key) { return configMap[key]; } }; ConfigManager* ConfigManager::instance = nullptr; int main() { ConfigManager::GetInstance().LoadConfig("db_host", "localhost"); ConfigManager::GetInstance().LoadConfig("db_port", "3306"); std::cout << "Database Host: " << ConfigManager::GetInstance().GetValue("db_host") << std::endl; std::cout << "Database Port: " << ConfigManager::GetInstance().GetValue("db_port") << std::endl; } ``` 这段代码展示了一个简单的配置管理系统,其中单例模式被用来保存检索配置项[^1]。 --- #### 4. **缓存机制** 某些情况下,可能需要维护一个全局可用的数据缓存结构。单例模式可以帮助我们构建这样的缓存系统。 ```cpp #include <unordered_map> #include <string> class Cache { private: static Cache* instance; std::unordered_map<std::string, std::string> cacheData; Cache() {} public: static Cache& GetInstance() { if (!instance) { instance = new Cache(); } return *instance; } bool SetCache(const std::string& key, const std::string& value) { cacheData[key] = value; return true; } std::string GetCache(const std::string& key) { auto it = cacheData.find(key); if (it != cacheData.end()) { return it->second; } return ""; } }; Cache* Cache::instance = nullptr; int main() { Cache::GetInstance().SetCache("key1", "value1"); std::cout << "Cached Value: " << Cache::GetInstance().GetCache("key1") << std::endl; } ``` 这里演示了如何利用单例模式实现一个基本的键值对缓存功能。 --- #### 5. **硬件驱动接口** 对于一些特定的硬件设备(如打印机、扫描仪),整个系统只需要与其建立一次通信链接即可满足需求。在这种情况下,单例模式能够很好地解决这一问题。 ```cpp class PrinterDriver { private: static PrinterDriver* instance; PrinterDriver() {} public: static PrinterDriver& GetInstance() { if (!instance) { instance = new PrinterDriver(); } return *instance; } void PrintDocument(const std::string& documentName) { std::cout << "Printing Document: " << documentName << std::endl; } }; PrinterDriver* PrinterDriver::instance = nullptr; int main() { PrinterDriver::GetInstance().PrintDocument("Report.pdf"); } ``` 该例子表明单例模式可用于封装唯一的硬件交互逻辑。 --- ### 总结 以上列举了几种常见的单例模式使用场景及其对应的 C++ 实现方式。每种情况均体现了单例模式的核心价值——即保证唯一性简化全局访问过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值