-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathTemplateMethod.php
81 lines (70 loc) · 1.63 KB
/
TemplateMethod.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
<?php
/*
*
*
* 模版方法:在一个方法中定义一个算法的框架,
* 规定要执行哪些算法,以及算法执行的顺序。
* 并且将一些算法的实现延迟到子类中。
*
* 这样做的好处是可以在不改变算法结构的基础上,
* 自由地处理一些算法的实现。
*
* 在本示例中,RunMachineTemplateMethod的run方法,
* 定义了一个算法框架,其中把openPower和display的
* 实现延迟到了子类中。示例中的两个子类是RunComputer
* 和RunTv。
* 需要注意的是,run方法要加final修饰符,防止子类修改
* 框架结构。connectElectricity方法也是可以在子类中
* 重载的。对于一些算法结构有不同分支的代码可以考虑
* 在算法结构中使用钩子方法实现。
*/
abstract class RunMachineTemplateMethod
{
final public function run()
{
$this->connectElectricity();
$this->openPower();
$this->display();
}
public function connectElectricity()
{
echo "The Electricity is coming!<br>\n";
}
abstract public function openPower();
abstract public function display();
}
class RunComputer extends RunMachineTemplateMethod
{
public function openPower()
{
echo "Computer open power!<br>\n";
}
public function display()
{
echo "Computer display!<br>\n";
}
}
class RunTv extends RunMachineTemplateMethod
{
public function openPower()
{
echo "TV open power!<br>\n";
}
public function display()
{
echo "TV display!<br>\n";
}
}
class Test
{
public function run()
{
$computer = new RunComputer();
$tv = new RunTv();
$computer->run();
echo "<hr>\n";
$tv->run();
}
}
$test = new Test();
$test->run();