3.11. Strategia (Strategy)¶
3.11.1. Terminologia¶
- Kontekst - ang. Context
- Strategia - ang. Strategy
- Konkretna Strategia - ang. Concrete Strategy
3.11.2. Przeznaczenie¶
Wzorzec projektowy Strategii pozwala definiować wymienne algorytmy i kapsułkować je w klasach. Dzięki temu możemy łatwo przełączać się między algorytmami. Ten wzorzec jest również dobrą alternatywą dla dziedziczenia - zamiast dziedziczenia po klasie abstrakcyjnej kolejne klasy implementują interfejs strategii.
3.11.3. Przykłady¶
- Sortowanie listy obiektów - jedną strategią może być sortowanie po dacie, drugą po id.
- Uproszczenie testów jednostkowych, na przykład poprzez zmianę miejsca przechowywania danych: w pliku lub w pamięci.
3.11.4. Diagram UML¶

3.11.5. Kod¶
Ten kod znajdziesz również na GitHub.
Context.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Strategy;
class Context
{
public function __construct(private Comparator $comparator)
{
}
public function executeStrategy(array $elements): array
{
uasort($elements, [$this->comparator, 'compare']);
return $elements;
}
}
|
Comparator.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Strategy;
interface Comparator
{
/**
* @param mixed $a
* @param mixed $b
*/
public function compare($a, $b): int;
}
|
DateComparator.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Strategy;
use DateTime;
class DateComparator implements Comparator
{
public function compare($a, $b): int
{
$aDate = new DateTime($a['date']);
$bDate = new DateTime($b['date']);
return $aDate <=> $bDate;
}
}
|
IdComparator.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Strategy;
class IdComparator implements Comparator
{
public function compare($a, $b): int
{
return $a['id'] <=> $b['id'];
}
}
|
3.11.6. Testy¶
Tests/StrategyTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Strategy\Tests;
use DesignPatterns\Behavioral\Strategy\Context;
use DesignPatterns\Behavioral\Strategy\DateComparator;
use DesignPatterns\Behavioral\Strategy\IdComparator;
use PHPUnit\Framework\TestCase;
class StrategyTest extends TestCase
{
public function provideIntegers()
{
return [
[
[['id' => 2], ['id' => 1], ['id' => 3]],
['id' => 1],
],
[
[['id' => 3], ['id' => 2], ['id' => 1]],
['id' => 1],
],
];
}
public function provideDates()
{
return [
[
[['date' => '2014-03-03'], ['date' => '2015-03-02'], ['date' => '2013-03-01']],
['date' => '2013-03-01'],
],
[
[['date' => '2014-02-03'], ['date' => '2013-02-01'], ['date' => '2015-02-02']],
['date' => '2013-02-01'],
],
];
}
/**
* @dataProvider provideIntegers
*
* @param array $collection
* @param array $expected
*/
public function testIdComparator($collection, $expected)
{
$obj = new Context(new IdComparator());
$elements = $obj->executeStrategy($collection);
$firstElement = array_shift($elements);
$this->assertSame($expected, $firstElement);
}
/**
* @dataProvider provideDates
*
* @param array $collection
* @param array $expected
*/
public function testDateComparator($collection, $expected)
{
$obj = new Context(new DateComparator());
$elements = $obj->executeStrategy($collection);
$firstElement = array_shift($elements);
$this->assertSame($expected, $firstElement);
}
}
|