3.10. Stan (State)¶
3.10.1. Przeznaczenie¶
Pozwala kapsułkować różne zachowania obiektu w ramach tego same procesu, w zależności od stanu w jakim obiekt aktualnie się znajduje. Dzięki temu nie musimy używać wielu instrukcji warunkowych.
3.10.2. Diagram UML¶

3.10.3. Kod¶
Ten kod znajdziesz również na GitHub.
OrderContext.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 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\State;
class OrderContext
{
private State $state;
public static function create(): OrderContext
{
$order = new self();
$order->state = new StateCreated();
return $order;
}
public function setState(State $state)
{
$this->state = $state;
}
public function proceedToNext()
{
$this->state->proceedToNext($this);
}
public function toString()
{
return $this->state->toString();
}
}
|
State.php
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\State;
interface State
{
public function proceedToNext(OrderContext $context);
public function toString(): string;
}
|
StateCreated.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\State;
class StateCreated implements State
{
public function proceedToNext(OrderContext $context)
{
$context->setState(new StateShipped());
}
public function toString(): string
{
return 'created';
}
}
|
StateShipped.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\State;
class StateShipped implements State
{
public function proceedToNext(OrderContext $context)
{
$context->setState(new StateDone());
}
public function toString(): string
{
return 'shipped';
}
}
|
StateDone.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\State;
class StateDone implements State
{
public function proceedToNext(OrderContext $context)
{
// there is nothing more to do
}
public function toString(): string
{
return 'done';
}
}
|
3.10.4. Testy¶
Tests/StateTest.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 | <?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\State\Tests;
use DesignPatterns\Behavioral\State\OrderContext;
use PHPUnit\Framework\TestCase;
class StateTest extends TestCase
{
public function testIsCreatedWithStateCreated()
{
$orderContext = OrderContext::create();
$this->assertSame('created', $orderContext->toString());
}
public function testCanProceedToStateShipped()
{
$contextOrder = OrderContext::create();
$contextOrder->proceedToNext();
$this->assertSame('shipped', $contextOrder->toString());
}
public function testCanProceedToStateDone()
{
$contextOrder = OrderContext::create();
$contextOrder->proceedToNext();
$contextOrder->proceedToNext();
$this->assertSame('done', $contextOrder->toString());
}
public function testStateDoneIsTheLastPossibleState()
{
$contextOrder = OrderContext::create();
$contextOrder->proceedToNext();
$contextOrder->proceedToNext();
$contextOrder->proceedToNext();
$this->assertSame('done', $contextOrder->toString());
}
}
|