目录
数据库连接池(Database Connection Pool)
单例模式是一种创建型设计模式,确保一个类只有一个实例,并提供一个全局访问点。单例模式常用于需要控制资源的共享访问,诸如数据库连接、日志管理器、配置管理等。
配置管理器在应用启动时加载配置,并在整个应用程序生命周期内提供访问。
- #include
- #include
- #include
- #include
-
- class ConfigManager {
- public:
- static ConfigManager& getInstance() {
- static ConfigManager instance;
- return instance;
- }
-
- void loadConfig(const std::string& configFile) {
- // 模拟加载配置文件
- configData["db_host"] = "localhost";
- configData["db_user"] = "admin";
- configData["db_password"] = "password";
- }
-
- std::string getConfigValue(const std::string& key) {
- return configData[key];
- }
-
- private:
- ConfigManager() {} // 私有构造函数
- ConfigManager(const ConfigManager&) = delete;
- ConfigManager& operator=(const ConfigManager&) = delete;
-
- std::unordered_map
configData; - };
-
- int main() {
- ConfigManager& config = ConfigManager::getInstance();
- config.loadConfig("config.txt");
-
- std::cout << "Database Host: " << config.getConfigValue("db_host") << std::endl;
- std::cout << "Database User: " << config.getConfigValue("db_user") << std::endl;
-
- return 0;
- }
日志管理器记录应用程序的日志信息,单例模式确保日志记录器在整个应用中是唯一的。
- #include
- #include
- #include
-
- class Logger {
- public:
- static Logger& getInstance() {
- static Logger instance;
- return instance;
- }
-
- void log(const std::string& message) {
- std::lock_guard
lock(mutex_) ; - logFile << message << std::endl;
- }
-
- private:
- Logger() {
- logFile.open("app.log", std::ios::app);
- }
-
- ~Logger() {
- if (logFile.is_open()) {
- logFile.close();
- }
- }
-
- Logger(const Logger&) = delete;
- Logger& operator=(const Logger&) = delete;
-
- std::ofstream logFile;
- std::mutex mutex_;
- };
-
- int main() {
- Logger& logger = Logger::getInstance();
- logger.log("Application started");
- logger.log("An error occurred");
-
- return 0;
- }
数据库连接池管理数据库连接的创建和复用,单例模式确保连接池在应用中只有一个实例。
- #include
- #include
- #include
- #include
-
- class DatabaseConnection {
- public:
- void connect(const std::string& connectionString) {
- std::cout << "Connecting to database: " << connectionString << std::endl;
- // 模拟连接操作
- }
-
- void disconnect() {
- std::cout << "Disconnecting from database" << std::endl;
- // 模拟断开操作
- }
- };
-
- class ConnectionPool {
- public:
- static ConnectionPool& getInstance() {
- static ConnectionPool instance;
- return instance;
- }
-
- std::shared_ptr
getConnection() { - std::lock_guard
lock(mutex_) ; - if (connections.empty()) {
- auto conn = std::make_shared
(); - conn->connect(connectionString);
- return conn;
- } else {
- auto conn = connections.back();
- connections.pop_back();
- return conn;
- }
- }
-
- void releaseConnection(std::shared_ptr
conn) { - std::lock_guard
lock(mutex_) ; - connections.push_back(conn);
- }
-
- private:
- ConnectionPool() : connectionString("DB_CONNECTION_STRING") {}
-
- ConnectionPool(const ConnectionPool&) = delete;
- ConnectionPool& operator=(const ConnectionPool&) = delete;
-
- std::string connectionString;
- std::vector
> connections; - std::mutex mutex_;
- };
-
- int main() {
- ConnectionPool& pool = ConnectionPool::getInstance();
- auto conn1 = pool.getConnection();
- auto conn2 = pool.getConnection();
-
- // 使用连接
- conn1->disconnect();
- pool.releaseConnection(conn1);
-
- return 0;
- }
我们在实际开发时会经常遇到此类设计模式 -- 单例模式,通过确保类的唯一实例来简化资源管理,并在多个场景中广泛应用,如配置管理、日志记录和数据库连接池。