designModeDemo/demo/Creational/Singleton/Run.php

41 lines
1.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace demo\Creational\Singleton;
class Run extends \bin\Design
{
/**
* 单例模式被公认为是 反面模式,为了获得更好的可测试性和可维护性,请使用『依赖注入模式』。
*
* [反面模式]在软件工程中一个反面模式Anti-pattern 或 Antipattern指的是在实践中明显出现但又低效或是有待优化的设计模式
* 是用来解决问题的带有共同性的不良方法。它们已经经过研究并分类,以防止日后重蹈覆辙,并能在研发尚未投产的系统时辨认出来。
*
* @inheritDoc
*/
public function setDesignName() : string
{
return '单例模式Singleton';
}
/**
* @inheritDoc
*/
public function setDesignRefUrl() : string
{
return 'https://learnku.com/docs/php-design-patterns/2018/Singleton/1494';
}
/**
* @inheritDoc
*/
public function main()
{
$firstCall = Singleton::getInstance();
$secondCall = Singleton::getInstance();
// 判断类型是否一致
dump(Singleton::class instanceof $firstCall);
// 判断是否一样
dump($firstCall === $secondCall);
}
}