-
Notifications
You must be signed in to change notification settings - Fork 4
/
behavioral-template-method.php
95 lines (82 loc) · 2.48 KB
/
behavioral-template-method.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* This example just to simulate how Template Method can be work
* 2nees.com
*/
/**
* Class CoffeeMachine - The Abstract Class defines a template method that contains a skeleton of some algorithm
*/
abstract class CoffeeMachine {
// This is the template which its define the skeleton of an algorithm - you cant update or change this template from subclass
final public function prepareCoffee(): void {
echo "Preparing Start..." . PHP_EOL;
echo $this->addCup() . PHP_EOL;
echo $this->addWater() . PHP_EOL;
echo $this->addMilk() . PHP_EOL;
echo $this->addSugar() . PHP_EOL;
echo "Done! you can got your Cup!" . PHP_EOL;
}
abstract protected function addWater(): string;
abstract protected function addSugar(): string;
abstract protected function addMilk(): string;
// Step, but can override!, this is the default cub for all subClasses...but its can override
protected function addCup(): string {
return "Add Carton Cub!";
}
}
#region Concrete Template Method - these class will implement different variations of Coffee type base on CoffeeMachine
class Coffee extends CoffeeMachine {
protected function addWater(): string
{
return "Add Hot Water to Coffee";
}
protected function addSugar(): string
{
return "Add Sugar to Coffee";
}
protected function addMilk(): string
{
return "skip milk...=>";// No milk on coffee
}
}
class HotChocolate extends CoffeeMachine {
protected function addWater(): string
{
return "Add Hot Water to HotChocolate";
}
protected function addSugar(): string
{
return "skip sugar...=>";// No Sugar on HotChocolate
}
protected function addMilk(): string
{
return "Add Milk";
}
}
class IceCoffee extends CoffeeMachine {
protected function addWater(): string
{
return "Add Cold Water to IceCoffee";
}
protected function addSugar(): string
{
return "Add a little bit of Sugar";
}
protected function addMilk(): string
{
return "Add Milk";
}
protected function addCup(): string {
return "Add Plastic Cub!";
}
}
#endregion
// Client
$coffee = new Coffee();
$coffee->prepareCoffee();
echo "============================" . PHP_EOL;
$hotChocolate = new HotChocolate();
$hotChocolate->prepareCoffee();
echo "============================" . PHP_EOL;
$iceCoffee = new IceCoffee();
$iceCoffee->prepareCoffee();