mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-05 06:27:25 +02:00
cs Strategy
This commit is contained in:
17
Strategy/ComparatorInterface.php
Normal file
17
Strategy/ComparatorInterface.php
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ComparatorInterface
|
||||||
|
*/
|
||||||
|
interface ComparatorInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param mixed $a
|
||||||
|
* @param mixed $b
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function compare($a, $b);
|
||||||
|
}
|
24
Strategy/DateComparator.php
Normal file
24
Strategy/DateComparator.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DateComparator
|
||||||
|
*/
|
||||||
|
class DateComparator implements ComparatorInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function compare($a, $b)
|
||||||
|
{
|
||||||
|
$aDate = strtotime($a['date']);
|
||||||
|
$bDate = strtotime($b['date']);
|
||||||
|
|
||||||
|
if ($aDate == $bDate) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return $aDate < $bDate ? -1 : 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
Strategy/IdComparator.php
Normal file
21
Strategy/IdComparator.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class IdComparator
|
||||||
|
*/
|
||||||
|
class IdComparator implements ComparatorInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function compare($a, $b)
|
||||||
|
{
|
||||||
|
if ($a['id'] == $b['id']) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return $a['id'] < $b['id'] ? -1 : 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
Strategy/ObjectCollection.php
Normal file
48
Strategy/ObjectCollection.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ObjectCollection
|
||||||
|
*/
|
||||||
|
class ObjectCollection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $elements;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ComparatorInterface
|
||||||
|
*/
|
||||||
|
private $comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $elements
|
||||||
|
*/
|
||||||
|
public function __construct(array $elements = array())
|
||||||
|
{
|
||||||
|
$this->elements = $elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function sort()
|
||||||
|
{
|
||||||
|
$callback = array($this->comparator, 'compare');
|
||||||
|
uasort($this->elements, $callback);
|
||||||
|
|
||||||
|
return $this->elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ComparatorInterface $comparator
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setComparator(ComparatorInterface $comparator)
|
||||||
|
{
|
||||||
|
$this->comparator = $comparator;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,111 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* strategy pattern
|
|
||||||
*
|
|
||||||
* Terminology:
|
|
||||||
* - Context
|
|
||||||
* - Strategy
|
|
||||||
* - Concrete Strategy
|
|
||||||
*
|
|
||||||
* Purpose:
|
|
||||||
* to separate strategies and to enable fast switching between them.
|
|
||||||
* also this pattern is a good alternative to inheritance (instead of having an abstract class that is extended)
|
|
||||||
*
|
|
||||||
* Examples:
|
|
||||||
* - sorting a list of objects, one strategy by date, the other by id
|
|
||||||
* - simplify unit testing: e.g. switching between file and in-memory storage
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
interface Comparator
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @abstract
|
|
||||||
* @param object $a
|
|
||||||
* @param object $b
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function compare($a, $b);
|
|
||||||
}
|
|
||||||
|
|
||||||
class ObjectCollection
|
|
||||||
{
|
|
||||||
private $_elements;
|
|
||||||
private $_comparator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $elements
|
|
||||||
*/
|
|
||||||
public function __construct(array $elements = array())
|
|
||||||
{
|
|
||||||
$this->_elements = $elements;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function sort()
|
|
||||||
{
|
|
||||||
$callback = array($this->_comparator, 'compare');
|
|
||||||
uasort($this->_elements, $callback);
|
|
||||||
|
|
||||||
return $this->_elements;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Comparator $comparator
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setComparator(Comparator $comparator)
|
|
||||||
{
|
|
||||||
$this->_comparator = $comparator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class IdComparator implements Comparator
|
|
||||||
{
|
|
||||||
public function compare($a, $b)
|
|
||||||
{
|
|
||||||
if ($a['id'] == $b['id']) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return $a['id'] < $b['id'] ? -1 : 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DateComparator implements Comparator
|
|
||||||
{
|
|
||||||
public function compare($a, $b)
|
|
||||||
{
|
|
||||||
$aDate = strtotime($a['date']);
|
|
||||||
$bDate = strtotime($b['date']);
|
|
||||||
|
|
||||||
if ($aDate == $bDate) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return $aDate < $bDate ? -1 : 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$elements = array(
|
|
||||||
array(
|
|
||||||
'id' => 2,
|
|
||||||
'date' => '2011-01-01',
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'id' => 1,
|
|
||||||
'date' => '2011-02-01'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$collection = new ObjectCollection($elements);
|
|
||||||
$collection->setComparator(new IdComparator());
|
|
||||||
$collection->sort();
|
|
||||||
|
|
||||||
$collection->setComparator(new DateComparator());
|
|
||||||
$collection->sort();
|
|
39
Strategy/index.php
Normal file
39
Strategy/index.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* strategy pattern
|
||||||
|
*
|
||||||
|
* Terminology:
|
||||||
|
* - Context
|
||||||
|
* - Strategy
|
||||||
|
* - Concrete Strategy
|
||||||
|
*
|
||||||
|
* Purpose:
|
||||||
|
* to separate strategies and to enable fast switching between them.
|
||||||
|
* also this pattern is a good alternative to inheritance (instead of having an abstract class that is extended)
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* - sorting a list of objects, one strategy by date, the other by id
|
||||||
|
* - simplify unit testing: e.g. switching between file and in-memory storage
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
$elements = array(
|
||||||
|
array(
|
||||||
|
'id' => 2,
|
||||||
|
'date' => '2011-01-01',
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 1,
|
||||||
|
'date' => '2011-02-01'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$collection = new ObjectCollection($elements);
|
||||||
|
$collection->setComparator(new IdComparator());
|
||||||
|
$collection->sort();
|
||||||
|
|
||||||
|
$collection->setComparator(new DateComparator());
|
||||||
|
$collection->sort();
|
Reference in New Issue
Block a user