Home > php教程 > php手册 > php设计模式 Template (模板模式)

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

WBOY
Release: 2016-06-21 08:54:20
Original
1275 people have browsed it

复制代码 代码如下:


/**
* 模板模式
*
* 定义一个操作中的算法骨架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构可以定义该算法的某些特定步骤
*
*/
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();
}
}

class TemplateObject extends TemplateBase
{
}

class TemplateObject1 extends TemplateBase
{
public function Method3()
{
echo "TemplateObject1 Method3
";
}
}

class TemplateObject2 extends TemplateBase
{
public function Method2()
{
echo "TemplateObject2 Method2
";
}
}

// 实例化
$objTemplate = new TemplateObject();
$objTemplate1 = new TemplateObject1();
$objTemplate2 = new TemplateObject2();

$objTemplate->doSomeThing();
$objTemplate1->doSomeThing();
$objTemplate2->doSomeThing();



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template