设计模式:模板方法模式

一、模板方法模式

  模板方法模式。定义为:Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.( 定义一个操作中的算法的框架, 而将一些步骤延迟到子类中。 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 )

<?php
    abstract class  Template{
        protected abstract function title();
        protected abstract function body();
public function show(){ echo "标题:" . $this->title() . PHP_EOL; echo "内容:" . $this->body() . PHP_EOL; } } class php_language extends Template { protected function title(){ return "我是PHP"; } protected function body(){ return "PHP是世界上最好的语言"; } } class html_language extends Template { protected function title(){ return "我是html"; } protected function body(){ return "html/css/js 网页三剑客"; } }

猜你喜欢

转载自www.cnblogs.com/onlycat/p/9164958.html