This commit is contained in:
Dominik Liebler
2013-09-13 12:00:39 +02:00
parent 2d3fc40a39
commit 644d9cbd49
12 changed files with 86 additions and 78 deletions

View File

@@ -1,18 +1,18 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject;
/**
* LoggerInterface is a contract for logging something
*
* Key-feaature : NullLogger MUST inherit from this interface like any other Loggers
* Key feature: NullLogger MUST inherit from this interface like any other Loggers
*/
interface LoggerInterface
{
/**
* @param string $str
*
* @return mixed
*/
public function log($str);
}
}

View File

@@ -1,9 +1,5 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject;
/**
@@ -32,10 +28,11 @@ namespace DesignPatterns\NullObject;
*/
class NullLogger implements LoggerInterface
{
/**
* @param string $str
*/
public function log($str)
{
// do nothing
}
}
}

View File

@@ -1,9 +1,5 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject;
/**
@@ -11,10 +7,11 @@ namespace DesignPatterns\NullObject;
*/
class PrintLogger implements LoggerInterface
{
/**
* @param string $str
*/
public function log($str)
{
echo $str;
}
}
}

View File

@@ -1,9 +1,5 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject;
/**
@@ -11,20 +7,28 @@ namespace DesignPatterns\NullObject;
*/
class Service
{
/**
* @var LoggerInterface
*/
protected $logger;
// we inject the logger in ctor and it is mandatory
/**
* we inject the logger in ctor and it is mandatory
*
* @param LoggerInterface $log
*/
public function __construct(LoggerInterface $log)
{
$this->logger = $log;
}
/**
* do something ...
*/
public function doSomething()
{
// no more check "if (!is_null($this->logger))..." with the NullObject pattern
$this->logger->log('We are in ' . __METHOD__);
// something to do...
}
}
}

View File

@@ -10,49 +10,57 @@ namespace DesignPatterns\Observer;
*/
class User implements \SplSubject
{
protected $_data = array();
/**
* user data
*
* @var array
*/
protected $_observers = array();
protected $data = array();
/**
* observers
*
* @var array
*/
protected $observers = array();
/**
* attach a new observer
*
* @param \SplObserver $observer
*
* @return void
*/
public function attach(\SplObserver $observer)
{
$this->_observers[] = $observer;
$this->observers[] = $observer;
}
/**
* detach an observer
*
* @param \SplObserver $observer
*
* @return void
*/
public function detach(\SplObserver $observer)
{
$index = array_search($observer, $this->_observers);
$index = array_search($observer, $this->observers);
if (false !== $index) {
unset($this->_observers[$index]);
unset($this->observers[$index]);
}
}
/**
*
* notify observers
*
* @return void
*/
public function notify()
{
/** @var SplObserver $observer */
foreach ($this->_observers as $observer) {
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
@@ -61,17 +69,16 @@ class User implements \SplSubject
* Ideally one would better write setter/getter for all valid attributes and only call notify()
* on attributes that matter when changed
*
* @param $name
* @param $value
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set($name, $value)
{
$this->_data[$name] = $value;
$this->data[$name] = $value;
// notify the observers, that user has been updated
$this->notify();
}
}

View File

@@ -17,7 +17,6 @@ namespace DesignPatterns\Observer;
*/
class UserObserver implements \SplObserver
{
/**
* This is the only method to implement as an observer.
* It is called by the Subject (usually by SplSubject::notify() )
@@ -28,5 +27,4 @@ class UserObserver implements \SplObserver
{
echo get_class($subject) . ' has been updated';
}
}

View File

@@ -18,14 +18,14 @@ class Record
/**
* @var array|null
*/
protected $_data;
protected $data;
/**
* @param null $data
*/
public function __construct($data = null)
{
$this->_data = (array) $data;
$this->data = (array) $data;
}
/**
@@ -38,7 +38,7 @@ class Record
*/
public function __set($name, $value)
{
$this->_data[(string) $name] = $value;
$this->data[(string) $name] = $value;
}
/**
@@ -50,8 +50,8 @@ class Record
*/
public function __get($name)
{
if (array_key_exists($name, $this->_data)) {
return $this->_data[(string) $name];
if (array_key_exists($name, $this->data)) {
return $this->data[(string) $name];
} else {
return null;
}

View File

@@ -47,4 +47,4 @@ class RecordProxy extends Record
$this->isDirty = true;
parent::__set($name, $value);
}
}
}

View File

@@ -14,39 +14,47 @@ namespace DesignPatterns;
* - Yii Framework: CWebApplication holds all the application components, such as CWebUser, CUrlManager, etc.
*
*/
abstract class Registry
{
const LOGGER = 'logger';
protected static $_storedValues;
/**
* @var array
*/
protected static $storedValues = array();
/**
* @static
* sets a value
*
* @param string $key
* @param mixed $value
* @param mixed $value
*
* @static
* @return void
*/
public static function set($key, $value)
{
self::$_storedValues[$key] = $value;
self::$storedValues[$key] = $value;
}
/**
* @static
* gets a value from the registry
*
* @param string $key
*
* @static
* @return mixed
*/
public static function get($key)
{
return self::$_storedValues[$key];
return self::$storedValues[$key];
}
// typically there would be methods to check if a key has already been registered and so on ...
}
// while bootstraping the application
// while bootstrapping the application
Registry::set(Registry::LOGGER, new \StdClass());
// throughout the application
Registry::get(Registry::LOGGER)->log('foo');
Registry::get(Registry::LOGGER)->log('foo');

View File

@@ -1,9 +1,5 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\SimpleFactory;
/**
@@ -11,10 +7,13 @@ namespace DesignPatterns\SimpleFactory;
*/
class Bicycle implements VehicleInterface
{
/**
* @param mixed $destination
*
* @return mixed|void
*/
public function driveTo($destination)
{
}
}
}
}

View File

@@ -1,9 +1,5 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\SimpleFactory;
/**
@@ -17,7 +13,9 @@ namespace DesignPatterns\SimpleFactory;
*/
class ConcreteFactory
{
/**
* @var array
*/
protected $typeList;
/**
@@ -36,6 +34,7 @@ class ConcreteFactory
* Creates a vehicle
*
* @param string $type a known type key
*
* @return VehicleInterface a new instance of VehicleInterface
* @throws \InvalidArgumentException
*/
@@ -48,5 +47,4 @@ class ConcreteFactory
return new $className();
}
}
}

View File

@@ -36,11 +36,11 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
public function testAttachDetach()
{
$subject = new User();
$this->assertAttributeEmpty('_observers', $subject);
$this->assertAttributeEmpty('observers', $subject);
$subject->attach($this->observer);
$this->assertAttributeNotEmpty('_observers', $subject);
$this->assertAttributeNotEmpty('observers', $subject);
$subject->detach($this->observer);
$this->assertAttributeEmpty('_observers', $subject);
$this->assertAttributeEmpty('observers', $subject);
}
/**