最近做项目感觉代码规划得不太好,于是想深入了解下设计模式。在这写写博客加深一下印象。首先来聊聊单例模式吧。 单例模式保证了全局该类只有一个实例,想多次使用该类的时候不需要频繁创建与销毁,节省了系统资源。
代码:
final class Singleton
{
private static $instance;
private function __construct()
{
}
private function __clone()
{
}
public static function getInstance()
{
if( !(
self::
$instance instanceof self) ) {
self::
$instance =
new self();
}
return self::
$instance;
}
public function test()
{
echo 'test';
}
}
$singleton = Singleton::getInstance();
$singleton->test();
流程图:
转载请注明原文地址: https://ju.6miu.com/read-600321.html