mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-09 00:16:32 +02:00
refactored State pattern example
This commit is contained in:
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user