mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 21:17:29 +02:00
refactored State pattern example
This commit is contained in:
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
|
||||||
|
|
||||||
class ContextOrder extends StateOrder
|
|
||||||
{
|
|
||||||
public function getState():StateOrder
|
|
||||||
{
|
|
||||||
return static::$state;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setState(StateOrder $state)
|
|
||||||
{
|
|
||||||
static::$state = $state;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function done()
|
|
||||||
{
|
|
||||||
static::$state->done();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getStatus(): string
|
|
||||||
{
|
|
||||||
return static::$state->getStatus();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
|
||||||
|
|
||||||
class CreateOrder extends StateOrder
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->setStatus('created');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function done()
|
|
||||||
{
|
|
||||||
static::$state = new ShippingOrder();
|
|
||||||
}
|
|
||||||
}
|
|
34
Behavioral/State/OrderContext.php
Normal file
34
Behavioral/State/OrderContext.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
|
class OrderContext
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var State
|
||||||
|
*/
|
||||||
|
private $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();
|
||||||
|
}
|
||||||
|
}
|
@@ -20,27 +20,33 @@ Code
|
|||||||
|
|
||||||
You can also find this code on `GitHub`_
|
You can also find this code on `GitHub`_
|
||||||
|
|
||||||
ContextOrder.php
|
OrderContext.php
|
||||||
|
|
||||||
.. literalinclude:: ContextOrder.php
|
.. literalinclude:: OrderContext.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
StateOrder.php
|
State.php
|
||||||
|
|
||||||
.. literalinclude:: StateOrder.php
|
.. literalinclude:: State.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
ShippingOrder.php
|
StateCreated.php
|
||||||
|
|
||||||
.. literalinclude:: ShippingOrder.php
|
.. literalinclude:: StateCreated.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
CreateOrder.php
|
StateShipped.php
|
||||||
|
|
||||||
.. literalinclude:: CreateOrder.php
|
.. literalinclude:: StateShipped.php
|
||||||
|
:language: php
|
||||||
|
:linenos:
|
||||||
|
|
||||||
|
StateDone.php
|
||||||
|
|
||||||
|
.. literalinclude:: StateDone.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
|
||||||
|
|
||||||
class ShippingOrder extends StateOrder
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->setStatus('shipping');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function done()
|
|
||||||
{
|
|
||||||
$this->setStatus('completed');
|
|
||||||
}
|
|
||||||
}
|
|
10
Behavioral/State/State.php
Normal file
10
Behavioral/State/State.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
|
interface State
|
||||||
|
{
|
||||||
|
public function proceedToNext(OrderContext $context);
|
||||||
|
|
||||||
|
public function toString(): string;
|
||||||
|
}
|
16
Behavioral/State/StateCreated.php
Normal file
16
Behavioral/State/StateCreated.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
|
class StateCreated implements State
|
||||||
|
{
|
||||||
|
public function proceedToNext(OrderContext $context)
|
||||||
|
{
|
||||||
|
$context->setState(new StateShipped());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return 'created';
|
||||||
|
}
|
||||||
|
}
|
16
Behavioral/State/StateDone.php
Normal file
16
Behavioral/State/StateDone.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
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';
|
||||||
|
}
|
||||||
|
}
|
@@ -1,32 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
|
||||||
|
|
||||||
abstract class StateOrder
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $details;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var StateOrder $state
|
|
||||||
*/
|
|
||||||
protected static $state;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
abstract protected function done();
|
|
||||||
|
|
||||||
protected function setStatus(string $status)
|
|
||||||
{
|
|
||||||
$this->details['status'] = $status;
|
|
||||||
$this->details['updatedTime'] = time();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getStatus(): string
|
|
||||||
{
|
|
||||||
return $this->details['status'];
|
|
||||||
}
|
|
||||||
}
|
|
16
Behavioral/State/StateShipped.php
Normal file
16
Behavioral/State/StateShipped.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
|
class StateShipped implements State
|
||||||
|
{
|
||||||
|
public function proceedToNext(OrderContext $context)
|
||||||
|
{
|
||||||
|
$context->setState(new StateDone());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return 'shipped';
|
||||||
|
}
|
||||||
|
}
|
@@ -2,30 +2,42 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State\Tests;
|
namespace DesignPatterns\Behavioral\State\Tests;
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\State\ContextOrder;
|
use DesignPatterns\Behavioral\State\OrderContext;
|
||||||
use DesignPatterns\Behavioral\State\CreateOrder;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class StateTest extends TestCase
|
class StateTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testCanShipCreatedOrder()
|
public function testIsCreatedWithStateCreated()
|
||||||
{
|
{
|
||||||
$order = new CreateOrder();
|
$orderContext = OrderContext::create();
|
||||||
$contextOrder = new ContextOrder();
|
|
||||||
$contextOrder->setState($order);
|
|
||||||
$contextOrder->done();
|
|
||||||
|
|
||||||
$this->assertEquals('shipping', $contextOrder->getStatus());
|
$this->assertEquals('created', $orderContext->toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCanCompleteShippedOrder()
|
public function testCanProceedToStateShipped()
|
||||||
{
|
{
|
||||||
$order = new CreateOrder();
|
$contextOrder = OrderContext::create();
|
||||||
$contextOrder = new ContextOrder();
|
$contextOrder->proceedToNext();
|
||||||
$contextOrder->setState($order);
|
|
||||||
$contextOrder->done();
|
|
||||||
$contextOrder->done();
|
|
||||||
|
|
||||||
$this->assertEquals('completed', $contextOrder->getStatus());
|
$this->assertEquals('shipped', $contextOrder->toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanProceedToStateDone()
|
||||||
|
{
|
||||||
|
$contextOrder = OrderContext::create();
|
||||||
|
$contextOrder->proceedToNext();
|
||||||
|
$contextOrder->proceedToNext();
|
||||||
|
|
||||||
|
$this->assertEquals('done', $contextOrder->toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStateDoneIsTheLastPossibleState()
|
||||||
|
{
|
||||||
|
$contextOrder = OrderContext::create();
|
||||||
|
$contextOrder->proceedToNext();
|
||||||
|
$contextOrder->proceedToNext();
|
||||||
|
$contextOrder->proceedToNext();
|
||||||
|
|
||||||
|
$this->assertEquals('done', $contextOrder->toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user