-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.php
More file actions
71 lines (64 loc) · 3.09 KB
/
Runner.php
File metadata and controls
71 lines (64 loc) · 3.09 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace App\InterfaceSegregation;
use App\InterfaceSegregation\Problem\DiscountItem as ProblemDiscountItem;
use App\InterfaceSegregation\Problem\SimpleItem as ProblemSimpleItem;
use App\InterfaceSegregation\Problem\PriceInterface as ProblemPriceInterface;
use App\InterfaceSegregation\Solution\DiscountItem as SolutionDiscountItem;
use App\InterfaceSegregation\Solution\SimpleItem as SolutionSimpleItem;
use App\InterfaceSegregation\Solution\PriceInterface as SolutionPriceInterface;
use App\InterfaceSegregation\Solution\DiscountInterface as SolutionDiscountInterface;
use App\InterfaceSegregation\Solution\AdditionalPriceInterface as SolutionAdditionalPriceInterface;
use App\RunnerInterface;
class Runner implements RunnerInterface
{
public function problemRun(): void
{
/** @var ProblemPriceInterface[] $items */
$items = [
'Simple Item:' => new ProblemSimpleItem(33.44),
'Discount Item:' => new ProblemDiscountItem(33.44, 0.3),
];
/**
* Main problem is a interface ProblemPriceInterface. Some classes which implement this interface have to implement
* methods which they will not use. For example, ProblemSimpleItem implements not used methods getDiscount() and getAdditionalPrice().
* ProblemDiscountItem implements not used method getAdditionalPrice().
*/
foreach ($items as $key => $item) {
echo $key.PHP_EOL;
if($item instanceof ProblemSimpleItem){
echo sprintf('Price %01.2f'.PHP_EOL, $item->getPrice());
}elseif($item instanceof ProblemDiscountItem){
echo sprintf('Price %01.2f'.PHP_EOL, $item->getPrice());
echo sprintf('Discount %01.2f'.PHP_EOL, $item->getDiscount());
}else{
echo sprintf('Price %01.2f'.PHP_EOL, $item->getPrice());
echo sprintf('Discount %01.2f'.PHP_EOL, $item->getDiscount());
echo sprintf('AdditionalPrice %01.2f'.PHP_EOL, $item->getAdditionalPrice());
}
}
}
public function solutionRun(): void
{
$items = [
'Simple Item:' => new SolutionSimpleItem(33.44),
'Discount Item:' => new SolutionDiscountItem(33.44, 0.3),
];
/**
* We divided ProblemPriceInterface into several interfaces (SolutionPriceInterface, SolutionDiscountInterface, SolutionAdditionalPriceInterface).
* As result - we implemented only the necessary methods
*/
foreach ($items as $key => $item) {
echo $key.PHP_EOL;
if($item instanceof SolutionPriceInterface){
echo sprintf('Price %01.2f'.PHP_EOL, $item->getPrice());
}
if($item instanceof SolutionDiscountInterface){
echo sprintf('Discount %01.2f'.PHP_EOL, $item->getDiscount());
}
if($item instanceof SolutionAdditionalPriceInterface){
echo sprintf('AdditionalPrice %01.2f'.PHP_EOL, $item->getAdditionalPrice());
}
}
}
}