remove Multiton

This commit is contained in:
Dominik Liebler
2019-12-14 13:55:04 +01:00
parent 579a5ac946
commit 62e6f5dbc8
26 changed files with 18 additions and 757 deletions

View File

@@ -1,44 +0,0 @@
<?php declare(strict_types=1);
namespace DesignPatterns\Creational\Multiton;
final class Multiton
{
const INSTANCE_1 = '1';
const INSTANCE_2 = '2';
/**
* @var Multiton[]
*/
private static array $instances = [];
/**
* this is private to prevent from creating arbitrary instances
*/
private function __construct()
{
}
public static function getInstance(string $instanceName): Multiton
{
if (!isset(self::$instances[$instanceName])) {
self::$instances[$instanceName] = new self();
}
return self::$instances[$instanceName];
}
/**
* prevent instance from being cloned
*/
private function __clone()
{
}
/**
* prevent instance from being unserialized
*/
private function __wakeup()
{
}
}

View File

@@ -1,46 +0,0 @@
Multiton
========
**THIS IS CONSIDERED TO BE AN ANTI-PATTERN! FOR BETTER TESTABILITY AND
MAINTAINABILITY USE DEPENDENCY INJECTION!**
Purpose
-------
To have only a list of named instances that are used, like a singleton
but with n instances.
Examples
--------
- 2 DB Connectors, e.g. one for MySQL, the other for SQLite
- multiple Loggers (one for debug messages, one for errors)
UML Diagram
-----------
.. image:: uml/uml.png
:alt: Alt Multiton UML Diagram
:align: center
Code
----
You can also find this code on `GitHub`_
Multiton.php
.. literalinclude:: Multiton.php
:language: php
:linenos:
Test
----
MultitonTest.php
.. literalinclude:: Tests/MultitonTest.php
:language: php
:linenos:
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Multiton

View File

@@ -1,28 +0,0 @@
<?php declare(strict_types=1);
namespace DesignPatterns\Creational\Multition\Tests;
use DesignPatterns\Creational\Multiton\Multiton;
use PHPUnit\Framework\TestCase;
class MultitonTest extends TestCase
{
public function testUniqueness()
{
$firstCall = Multiton::getInstance(Multiton::INSTANCE_1);
$secondCall = Multiton::getInstance(Multiton::INSTANCE_1);
$this->assertInstanceOf(Multiton::class, $firstCall);
$this->assertSame($firstCall, $secondCall);
}
public function testUniquenessForEveryInstance()
{
$firstCall = Multiton::getInstance(Multiton::INSTANCE_1);
$secondCall = Multiton::getInstance(Multiton::INSTANCE_2);
$this->assertInstanceOf(Multiton::class, $firstCall);
$this->assertInstanceOf(Multiton::class, $secondCall);
$this->assertNotSame($firstCall, $secondCall);
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

View File

@@ -9,7 +9,6 @@ solve this problem by somehow controlling this object creation.
* [AbstractFactory](AbstractFactory) [:notebook:](http://en.wikipedia.org/wiki/Abstract_factory_pattern)
* [Builder](Builder) [:notebook:](http://en.wikipedia.org/wiki/Builder_pattern)
* [FactoryMethod](FactoryMethod) [:notebook:](http://en.wikipedia.org/wiki/Factory_method_pattern)
* [Multiton](Multiton) (is considered an anti-pattern! :no_entry:)
* [Pool](Pool) [:notebook:](http://en.wikipedia.org/wiki/Object_pool_pattern)
* [Prototype](Prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern)
* [SimpleFactory](SimpleFactory)

View File

@@ -14,7 +14,6 @@ this object creation.
AbstractFactory/README
Builder/README
FactoryMethod/README
Multiton/README
Pool/README
Prototype/README
SimpleFactory/README

View File

@@ -14,8 +14,7 @@ Examples
--------
- DB Connector
- Logger (may also be a Multiton if there are many log files for
several purposes)
- Logger
- Lock file for the application (there is only one in the filesystem
...)