-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.php
More file actions
63 lines (47 loc) · 1.18 KB
/
Copy pathFactoryMethod.php
File metadata and controls
63 lines (47 loc) · 1.18 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
<?php
declare(strict_types = 1);
namespace FactoryMethod;
interface FruitInterface
{
public function getCalories(): float;
}
class Apple implements FruitInterface
{
public function getCalories(): float
{
$calories = 52 / 100;
return $calories;
}
}
class Orange implements FruitInterface
{
public function getCalories(): float
{
$calories = 47 / 100;
return $calories;
}
}
abstract class CaloriesCalculatorAbstract
{
public function calculateCalories(float $weight): float
{
$product = $this->makeFruit();
$calories = $product->getCalories();
$total = $calories * $weight;
return $total;
}
protected abstract function makeFruit(): FruitInterface;
}
class OrangeCaloriesCalculator extends CaloriesCalculatorAbstract
{
protected function makeFruit(): FruitInterface
{
$fruit = new Orange();
return $fruit;
}
}
$calories_calculator = new OrangeCaloriesCalculator();
$p_weight = 500;
$p_type = 'orange';
$total_calories = $calories_calculator->calculateCalories($p_weight);
echo sprintf('%dg. of %s has %.0F calories', $p_weight, $p_type, $total_calories);