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 <?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\NullObject; namespace DesignPatterns\NullObject;
/** /**
* LoggerInterface is a contract for logging something * 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 interface LoggerInterface
{ {
/**
* @param string $str
*
* @return mixed
*/
public function log($str); public function log($str);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -47,4 +47,4 @@ class RecordProxy extends Record
$this->isDirty = true; $this->isDirty = true;
parent::__set($name, $value); 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. * - Yii Framework: CWebApplication holds all the application components, such as CWebUser, CUrlManager, etc.
* *
*/ */
abstract class Registry abstract class Registry
{ {
const LOGGER = 'logger'; const LOGGER = 'logger';
protected static $_storedValues; /**
* @var array
*/
protected static $storedValues = array();
/** /**
* @static * sets a value
*
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
*
* @static
* @return void * @return void
*/ */
public static function set($key, $value) public static function set($key, $value)
{ {
self::$_storedValues[$key] = $value; self::$storedValues[$key] = $value;
} }
/** /**
* @static * gets a value from the registry
*
* @param string $key * @param string $key
*
* @static
* @return mixed * @return mixed
*/ */
public static function get($key) 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 ... // 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()); Registry::set(Registry::LOGGER, new \StdClass());
// throughout the application // throughout the application
Registry::get(Registry::LOGGER)->log('foo'); Registry::get(Registry::LOGGER)->log('foo');

View File

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

View File

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

View File

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