PHP7 Singleton und Multiton

This commit is contained in:
Dominik Liebler 2016-09-22 13:15:24 +02:00
parent 4cf817b143
commit 6a98bcb73b
5 changed files with 37 additions and 66 deletions

View File

@ -2,46 +2,26 @@
namespace DesignPatterns\Creational\Multiton; namespace DesignPatterns\Creational\Multiton;
/**
* class Multiton.
*/
class Multiton class Multiton
{ {
/**
* the first instance.
*/
const INSTANCE_1 = '1'; const INSTANCE_1 = '1';
/**
* the second instance.
*/
const INSTANCE_2 = '2'; const INSTANCE_2 = '2';
/** /**
* holds the named instances. * @var Multiton[]
*
* @var array
*/ */
private static $instances = array(); private static $instances = [];
/** /**
* should not be called from outside: private! * this is private to prevent from creating arbitrary instances
*/ */
private function __construct() private function __construct()
{ {
} }
/** public static function getInstance(string $instanceName): Multiton
* gets the instance with the given name, e.g. Multiton::INSTANCE_1
* uses lazy initialization.
*
* @param string $instanceName
*
* @return Multiton
*/
public static function getInstance($instanceName)
{ {
if (!array_key_exists($instanceName, self::$instances)) { if (!isset(self::$instances[$instanceName])) {
self::$instances[$instanceName] = new self(); self::$instances[$instanceName] = new self();
} }
@ -49,18 +29,14 @@ class Multiton
} }
/** /**
* prevent instance from being cloned. * prevent instance from being cloned
*
* @return void
*/ */
private function __clone() private function __clone()
{ {
} }
/** /**
* prevent instance from being unserialized. * prevent instance from being unserialized
*
* @return void
*/ */
private function __wakeup() private function __wakeup()
{ {

View File

@ -0,0 +1,27 @@
<?php
namespace DesignPatterns\Creational\Singleton\Tests;
use DesignPatterns\Creational\Multiton\Multiton;
class MultitonTest extends \PHPUnit_Framework_TestCase
{
public function testUniqueness()
{
$firstCall = Multiton::getInstance(Multiton::INSTANCE_1);
$secondCall = Multiton::getInstance(Multiton::INSTANCE_1);
$this->assertInstanceOf('DesignPatterns\Creational\Multiton\Multiton', $firstCall);
$this->assertSame($firstCall, $secondCall);
}
public function testUniquenessForEveryInstance()
{
$firstCall = Multiton::getInstance(Multiton::INSTANCE_1);
$secondCall = Multiton::getInstance(Multiton::INSTANCE_2);
$this->assertInstanceOf('DesignPatterns\Creational\Multiton\Multiton', $firstCall);
$this->assertInstanceOf('DesignPatterns\Creational\Multiton\Multiton', $secondCall);
$this->assertNotSame($firstCall, $secondCall);
}
}

View File

@ -2,7 +2,7 @@
namespace DesignPatterns\Creational\Singleton; namespace DesignPatterns\Creational\Singleton;
class Singleton final class Singleton
{ {
/** /**
* @var Singleton * @var Singleton
@ -31,21 +31,15 @@ class Singleton
/** /**
* prevent the instance from being cloned (which would create a second instance of it) * prevent the instance from being cloned (which would create a second instance of it)
*
* @throws SingletonPatternViolationException
*/ */
final public function __clone() private function __clone()
{ {
throw new \Exception('This is a Singleton. __clone is forbidden');
} }
/** /**
* prevent from being unserialized (which would create a second instance of it) * prevent from being unserialized (which would create a second instance of it)
*
* @throws SingletonPatternViolationException
*/ */
final public function __wakeup() private function __wakeup()
{ {
throw new \Exception('This is a Singleton. __wakeup is forbidden');
} }
} }

View File

@ -1,7 +0,0 @@
<?php
namespace DesignPatterns\Creational\Singleton;
class SingletonPatternViolationException extends \Exception
{
}

View File

@ -14,23 +14,4 @@ class SingletonTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall); $this->assertInstanceOf('DesignPatterns\Creational\Singleton\Singleton', $firstCall);
$this->assertSame($firstCall, $secondCall); $this->assertSame($firstCall, $secondCall);
} }
/**
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
*/
public function testNoCloneAllowed()
{
$obj1 = Singleton::getInstance();
$obj2 = clone $obj1;
}
/**
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
*/
public function testNoSerializationAllowed()
{
$obj1 = Singleton::getInstance();
$serialized = serialize($obj1);
unserialize($serialized);
}
} }