-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.php
More file actions
47 lines (38 loc) · 745 Bytes
/
strategy.php
File metadata and controls
47 lines (38 loc) · 745 Bytes
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
<?php
interface Strategy
{
public function process(int $x, int $y): int;
}
class Addition implements Strategy
{
public function process(int $x, int $y): int
{
return $x + $y;
}
}
class Subtraction implements Strategy
{
public function process(int $x, int $y): int
{
return $x - $y;
}
}
class Context
{
public function __construct(
private Strategy $strategy
) {
}
public function execute(int $x, int $y): int
{
return $this->strategy->process($x, $y);
}
}
/**
* Client
*/
$context = new Context(new Addition());
echo $context->execute(3, 4);
echo '<br>';
$contextSubtraction = new Context(new Subtraction());
echo $contextSubtraction->execute(3, 4);