mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-20 13:30:57 +02:00
cs Iterator and Proxy & Status
This commit is contained in:
parent
5852e62966
commit
af442a9980
@ -1,14 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Iterator;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* That's the key feature of this pattern :
|
||||
@ -27,12 +23,20 @@ namespace DesignPatterns\Iterator;
|
||||
*/
|
||||
class CardGame implements \Iterator
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $color = array('D', 'S', 'C', 'H');
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $number = array(7, 8, 9, 10, 'J', 'Q', 'K', 'A');
|
||||
|
||||
/**
|
||||
* Return the current value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
@ -41,6 +45,8 @@ class CardGame implements \Iterator
|
||||
|
||||
/**
|
||||
* Return the current key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
@ -71,10 +77,11 @@ class CardGame implements \Iterator
|
||||
/**
|
||||
* Is the current position a valid item (true)
|
||||
* or do we reach the end (false) ?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return current($this->number) || current($this->color);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -21,20 +21,29 @@ namespace DesignPatterns;
|
||||
*/
|
||||
class File
|
||||
{
|
||||
protected $_rowset;
|
||||
/**
|
||||
* @var Rowset
|
||||
*/
|
||||
protected $rowset;
|
||||
|
||||
protected $_pathName;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $pathName;
|
||||
|
||||
/**
|
||||
* @param string $pathName
|
||||
*/
|
||||
public function __construct($pathName)
|
||||
{
|
||||
$this->_rowset = new Rowset($this);
|
||||
$this->rowset = new Rowset($this);
|
||||
}
|
||||
|
||||
public function process()
|
||||
{
|
||||
// this is the place to show how using an iterator, with foreach
|
||||
// See the CardGame.php file
|
||||
$this->_rowset->process();
|
||||
$this->rowset->process();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
59
Proxy/Record.php
Normal 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
50
Proxy/RecordProxy.php
Normal 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);
|
||||
}
|
||||
}
|
@ -10,8 +10,10 @@ namespace DesignPatterns;
|
||||
*/
|
||||
interface OrderInterface
|
||||
{
|
||||
|
||||
public function shipOrder();
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function shipOrder();
|
||||
|
||||
public function completeOrder();
|
||||
|
||||
@ -100,9 +102,12 @@ class OrderFactory {
|
||||
|
||||
// Client
|
||||
|
||||
Class OrderControler {
|
||||
|
||||
public function shipAction($id)
|
||||
class OrderController
|
||||
{
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function shipAction($id)
|
||||
{
|
||||
$order = OrderFactory::getOrder($id);
|
||||
try {
|
||||
@ -123,5 +128,4 @@ Class OrderControler {
|
||||
}
|
||||
// response to browser
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user