start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View File

@@ -0,0 +1,74 @@
<?php
namespace DesignPatterns;
/**
* class Multiton
*/
class Multiton
{
/**
*
* the first instance
*/
const INSTANCE_1 = '1';
/**
*
* the second instance
*/
const INSTANCE_2 = '2';
/**
* holds the named instances
*
* @var array
*/
private static $instances = array();
/**
* should not be called from outside: private!
*
*/
private function __construct()
{
}
/**
* 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)) {
self::$instances[$instanceName] = new self();
}
return self::$instances[$instanceName];
}
/**
* prevent instance from being cloned
*
* @return void
*/
private function __clone()
{
}
/**
* prevent instance from being unserialized
*
* @return void
*/
private function __wakeup()
{
}
}

View File

@@ -0,0 +1,12 @@
# 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)