cs Iterator and Proxy & Status

This commit is contained in:
Dominik Liebler 2013-09-11 16:50:01 +02:00
parent 5852e62966
commit af442a9980
6 changed files with 147 additions and 112 deletions

View File

@ -1,14 +1,10 @@
<?php <?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Iterator; namespace DesignPatterns\Iterator;
/** /**
* Iterator provides a standard way to iterate over a collection without knowing * Iterator provides a standard way to iterate over a collection without knowing
* how it is implemented. All you need to know is : you can traverse it * how it is implemented. All you need to know is that you can traverse it
* with current, valid, next, rewind and key. * with current, valid, next, rewind and key.
* *
* That's the key feature of this pattern : * That's the key feature of this pattern :
@ -27,12 +23,20 @@ namespace DesignPatterns\Iterator;
*/ */
class CardGame implements \Iterator class CardGame implements \Iterator
{ {
/**
* @var array
*/
protected $color = array('D', 'S', 'C', 'H'); protected $color = array('D', 'S', 'C', 'H');
/**
* @var array
*/
protected $number = array(7, 8, 9, 10, 'J', 'Q', 'K', 'A'); protected $number = array(7, 8, 9, 10, 'J', 'Q', 'K', 'A');
/** /**
* Return the current value * Return the current value
*
* @return string
*/ */
public function current() public function current()
{ {
@ -41,6 +45,8 @@ class CardGame implements \Iterator
/** /**
* Return the current key * Return the current key
*
* @return string
*/ */
public function key() public function key()
{ {
@ -71,10 +77,11 @@ class CardGame implements \Iterator
/** /**
* Is the current position a valid item (true) * Is the current position a valid item (true)
* or do we reach the end (false) ? * or do we reach the end (false) ?
*
* @return boolean
*/ */
public function valid() public function valid()
{ {
return current($this->number) || current($this->color); return current($this->number) || current($this->color);
} }
}
}

View File

@ -21,20 +21,29 @@ namespace DesignPatterns;
*/ */
class File class File
{ {
protected $_rowset; /**
* @var Rowset
*/
protected $rowset;
protected $_pathName; /**
* @var string
*/
protected $pathName;
/**
* @param string $pathName
*/
public function __construct($pathName) public function __construct($pathName)
{ {
$this->_rowset = new Rowset($this); $this->rowset = new Rowset($this);
} }
public function process() public function process()
{ {
// this is the place to show how using an iterator, with foreach // this is the place to show how using an iterator, with foreach
// See the CardGame.php file // See the CardGame.php file
$this->_rowset->process(); $this->rowset->process();
} }
} }

View File

@ -1,94 +0,0 @@
<?php
namespace DesignPatterns;
/**
* Proxy pattern
*
* Purpose:
* to interface to anything that is expensive or impossible to duplicate
*
* Examples:
* - Doctrine2 uses proxies to implement framework magic (e.g. Lazy initialization) in them, while the user still works
* with his own entity classes and will never use nor touch the proxies
*
*/
class Record
{
protected $_data;
public function __construct($data = null)
{
$this->_data = $data;
}
/**
* magic setter
*
* @param string $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
{
$this->_data[(string) $name] = $value;
}
/**
* magic getter
*
* @param string $name
* @return mixed|null
*/
public function __get($name)
{
if (array_key_exists($name, $this->_data)) {
return $this->_data[(string) $name];
} else {
return null;
}
}
}
class RecordProxy extends Record
{
/**
* @var bool
*/
protected $_isDirty = false;
/**
* @var bool
*/
protected $_isInitialized = false;
/**
* @param array
*/
public function __construct($data)
{
parent::__construct($data);
// when the record has data, mark it as initialized
// since Record will hold our business logic, we don't want to
// implement this behaviour there, but instead in a new proxy class
// that extends the Record class
if (null !== $data) {
$this->_isInitialized = true;
$this->_isDirty = true;
}
}
/**
* magic setter
*
* @param string $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
{
$this->_isDirty = true;
parent::__set($name, $value);
}
}

59
Proxy/Record.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace DesignPatterns\Proxy;
/**
* Proxy pattern
*
* Purpose:
* to interface to anything that is expensive or impossible to duplicate
*
* Examples:
* - Doctrine2 uses proxies to implement framework magic (e.g. lazy initialization) in them, while the user still works
* with his own entity classes and will never use nor touch the proxies
*
*/
class Record
{
/**
* @var array|null
*/
protected $_data;
/**
* @param null $data
*/
public function __construct($data = null)
{
$this->_data = (array) $data;
}
/**
* magic setter
*
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set($name, $value)
{
$this->_data[(string) $name] = $value;
}
/**
* magic getter
*
* @param string $name
*
* @return mixed|null
*/
public function __get($name)
{
if (array_key_exists($name, $this->_data)) {
return $this->_data[(string) $name];
} else {
return null;
}
}
}

50
Proxy/RecordProxy.php Normal file
View File

@ -0,0 +1,50 @@
<?php
namespace DesignPatterns\Proxy;
/**
* Class RecordProxy
*/
class RecordProxy extends Record
{
/**
* @var bool
*/
protected $isDirty = false;
/**
* @var bool
*/
protected $isInitialized = false;
/**
* @param array $data
*/
public function __construct($data)
{
parent::__construct($data);
// when the record has data, mark it as initialized
// since Record will hold our business logic, we don't want to
// implement this behaviour there, but instead in a new proxy class
// that extends the Record class
if (null !== $data) {
$this->isInitialized = true;
$this->isDirty = true;
}
}
/**
* magic setter
*
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set($name, $value)
{
$this->isDirty = true;
parent::__set($name, $value);
}
}

View File

@ -10,8 +10,10 @@ namespace DesignPatterns;
*/ */
interface OrderInterface interface OrderInterface
{ {
/**
public function shipOrder(); * @return mixed
*/
public function shipOrder();
public function completeOrder(); public function completeOrder();
@ -100,9 +102,12 @@ class OrderFactory {
// Client // Client
Class OrderControler { class OrderController
{
public function shipAction($id) /**
* @param int $id
*/
public function shipAction($id)
{ {
$order = OrderFactory::getOrder($id); $order = OrderFactory::getOrder($id);
try { try {
@ -123,5 +128,4 @@ Class OrderControler {
} }
// response to browser // response to browser
} }
} }