框架推迟某个该模式在一个操作中定义了算法的框架将某些步骤推迟到子类中它允许子类在不改变一个算法的结构的情况下重新定义某些步骤
ConcreteClass(具体类)
This class implements the primitive(原始的,简单的) operations to carry out subclass-specific steps of the algorithm.
(这个类给执行算法中子类特有的的步骤实现了简单的操作。)
AbstractClass(抽像类)
This class (a) defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm, and (b) implements a template method defining the skeleton of an algorithm.
(这类(a)定义了子类中具体的简单操作的抽象给算法的实施步骤,和(b)实现了一个模板方法定义算法的框架。)
<?php
class TemplateObject extends TemplateBase
{
}
class TemplateObject1 extends TemplateBase
{
public function Method3() {
echo "TemplateObject1 Method3";
}
}
class TemplateObject2 extends TemplateBase
{
public function Method2() {
echo "TemplateObject2 Method2";
}
}
abstract class TemplateBase {
public function Method1() {
echo "abstract Method1";
}
public function Method2() {
echo "abstract Method2";
}
public function Method3() {
echo "abstract Method3";
}
public function doSomeThing() {
$this->Method1();
$this->Method2();
$this->Method3();
}
}
$objTemplate = new TemplateObject();
$objTemplate1 = new TemplateObject1();
$objTemplate2 = new TemplateObject2();
$objTemplate->doSomeThing();
$objTemplate1->doSomeThing();
$objTemplate2->doSomeThing();
评论