Skip to content

Commit 93d4ebf

Browse files
author
Dominik Liebler
committed
refactored State pattern example
1 parent ea85485 commit 93d4ebf

File tree

11 files changed

+133
-113
lines changed

11 files changed

+133
-113
lines changed

Behavioral/State/ContextOrder.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

Behavioral/State/CreateOrder.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

Behavioral/State/OrderContext.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
class OrderContext
6+
{
7+
/**
8+
* @var State
9+
*/
10+
private $state;
11+
12+
public static function create(): OrderContext
13+
{
14+
$order = new self();
15+
$order->state = new StateCreated();
16+
17+
return $order;
18+
}
19+
20+
public function setState(State $state)
21+
{
22+
$this->state = $state;
23+
}
24+
25+
public function proceedToNext()
26+
{
27+
$this->state->proceedToNext($this);
28+
}
29+
30+
public function toString()
31+
{
32+
return $this->state->toString();
33+
}
34+
}

Behavioral/State/README.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,33 @@ Code
2020

2121
You can also find this code on `GitHub`_
2222

23-
ContextOrder.php
23+
OrderContext.php
2424

25-
.. literalinclude:: ContextOrder.php
25+
.. literalinclude:: OrderContext.php
2626
:language: php
2727
:linenos:
2828

29-
StateOrder.php
29+
State.php
3030

31-
.. literalinclude:: StateOrder.php
31+
.. literalinclude:: State.php
3232
:language: php
3333
:linenos:
3434

35-
ShippingOrder.php
35+
StateCreated.php
3636

37-
.. literalinclude:: ShippingOrder.php
37+
.. literalinclude:: StateCreated.php
3838
:language: php
3939
:linenos:
4040

41-
CreateOrder.php
41+
StateShipped.php
4242

43-
.. literalinclude:: CreateOrder.php
43+
.. literalinclude:: StateShipped.php
44+
:language: php
45+
:linenos:
46+
47+
StateDone.php
48+
49+
.. literalinclude:: StateDone.php
4450
:language: php
4551
:linenos:
4652

Behavioral/State/ShippingOrder.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

Behavioral/State/State.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
interface State
6+
{
7+
public function proceedToNext(OrderContext $context);
8+
9+
public function toString(): string;
10+
}

Behavioral/State/StateCreated.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
class StateCreated implements State
6+
{
7+
public function proceedToNext(OrderContext $context)
8+
{
9+
$context->setState(new StateShipped());
10+
}
11+
12+
public function toString(): string
13+
{
14+
return 'created';
15+
}
16+
}

Behavioral/State/StateDone.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
class StateDone implements State
6+
{
7+
public function proceedToNext(OrderContext $context)
8+
{
9+
// there is nothing more to do
10+
}
11+
12+
public function toString(): string
13+
{
14+
return 'done';
15+
}
16+
}

Behavioral/State/StateOrder.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

Behavioral/State/StateShipped.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
class StateShipped implements State
6+
{
7+
public function proceedToNext(OrderContext $context)
8+
{
9+
$context->setState(new StateDone());
10+
}
11+
12+
public function toString(): string
13+
{
14+
return 'shipped';
15+
}
16+
}

0 commit comments

Comments
 (0)