单例模式(Singleton Pattern)是一种设计模式,用于确保一个类只有一个实例,并提供一个全局访问点来访问该实例。在PHP中,单例模式的应用场景主要包括以下几种:
解决方案:使用单例模式确保在整个应用程序中只有一个数据库连接实例,所有需要访问数据库的地方都共享这个连接。
class DatabaseConnection {
private static $instance = null;
private $connection;
private function __construct() {
$this->connection = new PDO("mysql:host=localhost;dbname=test", "user", "password");
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new DatabaseConnection();
}
return self::$instance;
}
public function getConnection() {
return $this->connection;
}
}
$db = DatabaseConnection::getInstance();
$conn = $db->getConnection();
解决方案:使用单例模式确保配置信息只加载一次,并在需要时通过单例对象访问。
class Configuration {
private static $instance = null;
private $config = [];
private function __construct() {
$this->config = parse_ini_file('config.ini');
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Configuration();
}
return self::$instance;
}
public function get($key) {
return $this->config[$key] ?? null;
}
}
$config = Configuration::getInstance();
$dbHost = $config->get('db_host');
解决方案:使用单例模式确保只有一个日志记录器实例,所有日志都通过这个实例写入。
class Logger {
private static $instance = null;
private $logFile;
private function __construct() {
$this->logFile = fopen('app.log', 'a');
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Logger();
}
return self::$instance;
}
public function log($message) {
fwrite($this->logFile, $message . PHP_EOL);
}
public function __destruct() {
fclose($this->logFile);
}
}
$logger = Logger::getInstance();
$logger->log("This is a log message.");
解决方案:使用单例模式确保只有一个缓存实例,所有缓存操作都通过这个实例进行。
class Cache {
private static $instance = null;
private $cache = [];
private function __construct() {}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Cache();
}
return self::$instance;
}
public function set($key, $value) {
$this->cache[$key] = $value;
}
public function get($key) {
return $this->cache[$key] ?? null;
}
}
$cache = Cache::getInstance();
$cache->set('user_1', 'John Doe');
$user = $cache->get('user_1');
解决方案:使用单例模式确保只有一个线程池实例,所有线程操作都通过这个实例进行。
class ThreadPool {
private static $instance = null;
private $pool = [];
private function __construct() {}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new ThreadPool();
}
return self::$instance;
}
public function addThread($thread) {
$this->pool[] = $thread;
}
public function run() {
foreach ($this->pool as $thread) {
$thread->start();
}
}
}
$threadPool = ThreadPool::getInstance();
$threadPool->addThread(new Thread());
$threadPool->run();
解决方案:使用单例模式确保服务定位器只有一个实例,所有服务都通过这个实例访问。
class ServiceLocator {
private static $instance = null;
private $services = [];
private function __construct() {}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new ServiceLocator();
}
return self::$instance;
}
public function register($name, $service) {
$this->services[$name] = $service;
}
public function get($name) {
return $this->services[$name] ?? null;
}
}
$serviceLocator = ServiceLocator::getInstance();
$serviceLocator->register('db', new DatabaseConnection());
$db = $serviceLocator->get('db');
单例模式在PHP中的应用场景主要集中在需要全局唯一实例的情况下,如数据库连接、配置管理、日志记录、缓存管理、线程池管理和服务定位器等。通过使用单例模式,可以有效地减少资源消耗、提高性能,并确保数据的一致性和安全性。