[php]php设计模式 Template (模板模式)

1 <?php
2 /**
3 * 模板模式
4 *
5 * 定义一个操作中的算法骨架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构可以定义该算法的某些特定步骤
6 *
7 */
8 abstractclass TemplateBase
9 {
10 publicfunction Method1()
11 {
12 echo"abstract Method1<br/>";
13 }
14
15 publicfunction Method2()
16 {
17 echo"abstract Method2<br/>";
18 }
19
20 publicfunction Method3()
21 {
22 echo"abstract Method3<br/>";
23 }
24
25 publicfunction doSomeThing()
26 {
27 $this->Method1();
28 $this->Method2();
29 $this->Method3();
30 }
31 }
32
33 class TemplateObject extends TemplateBase
34 {
35 }
36
37 class TemplateObject1 extends TemplateBase
38 {
39 publicfunction Method3()
40 {
41 echo"TemplateObject1 Method3<br/>";
42 }
43 }
44
45 class TemplateObject2 extends TemplateBase
46 {
47 publicfunction Method2()
48 {
49 echo"TemplateObject2 Method2<br/>";
50 }
51 }
52
53 // 实例化
54 $objTemplate=new TemplateObject();
55 $objTemplate1=new TemplateObject1();
56 $objTemplate2=new TemplateObject2();
57
58 $objTemplate->doSomeThing();
59 $objTemplate1->doSomeThing();
60 $objTemplate2->doSomeThing();
原文地址:https://www.cnblogs.com/bluefrog/p/2083241.html