模版模式属于行为型模式
概述:定义一个操作的算法骨架,而将一些步骤延迟到子类中,模版方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤
<?php
/**
*模版模式基类
*/
abstract class Template{
public function templateA(){}
public function templateB(){}
public function getTemplate(){
$this->templateA();
$this->templateB();
}
}
<?php
/**
* 子类
*/
class TemplateChild extends Template{
public function templateA(){
echo ‘A’;
}
public function templateB(){
echo ‘B’;
}
}
<?php
/**
*模版方法
*/
public function actionTemplate(){
Yii::import(‘ext.template.*’);
$templateC = new TemplateChild;
$templateC->getTemplate();
}