diff --git a/Creational/Singleton/Singleton.php b/Creational/Singleton/Singleton.php index 650810e..e5d7ce0 100644 --- a/Creational/Singleton/Singleton.php +++ b/Creational/Singleton/Singleton.php @@ -2,22 +2,17 @@ namespace DesignPatterns\Creational\Singleton; -/** - * class Singleton. - */ class Singleton { /** - * @var Singleton reference to singleton instance + * @var Singleton */ private static $instance; /** - * gets the instance via lazy initialization (created on first usage). - * - * @return self + * gets the instance via lazy initialization (created on first usage) */ - public static function getInstance() + public static function getInstance(): Singleton { if (null === static::$instance) { static::$instance = new static(); @@ -27,33 +22,30 @@ class Singleton } /** - * is not allowed to call from outside: private! + * is not allowed to call from outside to prevent from creating multiple instances, + * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead */ private function __construct() { } /** - * prevent the instance from being cloned. + * prevent the instance from being cloned (which would create a second instance of it) * * @throws SingletonPatternViolationException - * - * @return void */ final public function __clone() { - throw new SingletonPatternViolationException('This is a Singleton. Clone is forbidden'); + throw new \Exception('This is a Singleton. __clone is forbidden'); } /** - * prevent from being unserialized. + * prevent from being unserialized (which would create a second instance of it) * * @throws SingletonPatternViolationException - * - * @return void */ final public function __wakeup() { - throw new SingletonPatternViolationException('This is a Singleton. __wakeup usage is forbidden'); + throw new \Exception('This is a Singleton. __wakeup is forbidden'); } } diff --git a/Creational/Singleton/Tests/SingletonTest.php b/Creational/Singleton/Tests/SingletonTest.php index 6b72285..a3be46c 100644 --- a/Creational/Singleton/Tests/SingletonTest.php +++ b/Creational/Singleton/Tests/SingletonTest.php @@ -4,32 +4,19 @@ namespace DesignPatterns\Creational\Singleton\Tests; use DesignPatterns\Creational\Singleton\Singleton; -/** - * SingletonTest tests the singleton pattern. - */ class SingletonTest extends \PHPUnit_Framework_TestCase { public function testUniqueness() { $firstCall = Singleton::getInstance(); - $this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall); $secondCall = Singleton::getInstance(); + + $this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall); $this->assertSame($firstCall, $secondCall); } - public function testNoConstructor() - { - $obj = Singleton::getInstance(); - - $refl = new \ReflectionObject($obj); - $meth = $refl->getMethod('__construct'); - $this->assertTrue($meth->isPrivate()); - } - /** * @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException - * - * @return void */ public function testNoCloneAllowed() { @@ -39,13 +26,11 @@ class SingletonTest extends \PHPUnit_Framework_TestCase /** * @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException - * - * @return void */ public function testNoSerializationAllowed() { $obj1 = Singleton::getInstance(); $serialized = serialize($obj1); - $obj2 = unserialize($serialized); + unserialize($serialized); } }