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,17 @@
# Singleton
**THIS IS CONSIDERED TO BE AN ANTI-PATTERN! FOR BETTER TESTABILITY AND MAINTAINABILITY USE DEPENDENCY INJECTION!**
## Purpose
To have only one instance of this object in the application that will handle all calls.
## Examples
* DB Connector
* Logger (may also be a Multiton if there are many log files for several purposes)
* Lock file for the application (there is only one in the filesystem ...)
## Diagram
<img src="http://yuml.me/diagram/scruffy/class/[Singleton|-instance: Singleton|+getInstance(): Singleton;-__construct(): void;-__clone(): void;-__wakeup(): void]" >

View File

@@ -0,0 +1,58 @@
<?php
namespace DesignPatterns\Singleton;
/**
* class Singleton
*/
class Singleton
{
/**
* @var cached reference to singleton instance
*/
protected static $instance;
/**
* gets the instance via lazy initialization (created on first usage)
*
* @return self
*/
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static;
}
return static::$instance;
}
/**
* is not allowed to call from outside: private!
*
*/
private function __construct()
{
}
/**
* prevent the instance from being cloned
*
* @return void
*/
private function __clone()
{
}
/**
* prevent from being unserialized
*
* @return void
*/
private function __wakeup()
{
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace DesignPatterns\Tests\Singleton;
use DesignPatterns\Singleton\Singleton;
/**
* SingletonTest tests the singleton pattern
*/
class SingletonTest extends \PHPUnit_Framework_TestCase
{
public function testUniqueness()
{
$firstCall = Singleton::getInstance();
$this->assertInstanceOf('DesignPatterns\Singleton\Singleton', $firstCall);
$secondCall = Singleton::getInstance();
$this->assertSame($firstCall, $secondCall);
}
public function testNoConstructor()
{
$obj = Singleton::getInstance();
$refl = new \ReflectionObject($obj);
$meth = $refl->getMethod('__construct');
$this->assertTrue($meth->isPrivate());
}
}