博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP设计模式学习笔记: 模版方法
阅读量:7102 次
发布时间:2019-06-28

本文共 2002 字,大约阅读时间需要 6 分钟。

  hot3.png

翻译: 

http://sourcemaking.com/design_patterns/template_method

http://sourcemaking.com/design_patterns/template_method/php

abstract class TemplateAbstract {    public final function showBookTitleInfo($book_in) {        $title = $book_in->getTitle();        $author = $book_in->getAuthor();        $processedTitle = $this->processTitle($title);        $processedAuthor = $this->processAuthor($author);        if (NULL == $processedAuthor) {            $processed_info = $processedTitle;        } else {            $processed_info = $processedTitle.' by '.$processedAuthor;        }        return $processed_info;    }     abstract function processTitle($title);      function processAuthor($author) {        return NULL;    } }class TemplateExclaim extends TemplateAbstract {    function processTitle($title) {      return str_replace(' ','!!!',$title);     }    function processAuthor($author) {      return str_replace(' ','!!!',$author);    }}class TemplateStars extends TemplateAbstract {    function processTitle($title) {        return str_replace(' ','*',$title);     }}class Book {    private $author;    private $title;    function __construct($title_in, $author_in) {        $this->author = $author_in;        $this->title  = $title_in;    }    function getAuthor() {return $this->author;}    function getTitle() {return $this->title;}    function getAuthorAndTitle() {        return $this->getTitle() . ' by ' . $this->getAuthor();    }}  writeln('开始测试模版方法');echo PHP_EOL;   $book = new Book('PHP for Cats','Larry Truett');   $exclaimTemplate = new TemplateExclaim();    $starsTemplate = new TemplateStars();   writeln('test 1 - show exclaim template');echo PHP_EOL;  writeln($exclaimTemplate->showBookTitleInfo($book));echo PHP_EOL;  writeln('test 2 - show stars template');echo PHP_EOL;  writeln($starsTemplate->showBookTitleInfo($book));echo PHP_EOL;  writeln('END TESTING TEMPLATE PATTERN');  function writeln($line_in) {    echo $line_in;  }

todo

转载于:https://my.oschina.net/ecnu/blog/286330

你可能感兴趣的文章
组合数学 - 母函数的运用 --- 模板题
查看>>
检测MYSQL不同步发邮件通知的脚本
查看>>
Struts2学习笔记1
查看>>
python的ftp上传和下载
查看>>
ASP.NET MVC 中的路由
查看>>
微信公众平台帐号通过昵称无法搜索到怎么办
查看>>
Oracle笔记 六、PL/SQL简单语句块、变量定义
查看>>
Linux 常用命令
查看>>
何为蠕虫病毒
查看>>
[詹兴致矩阵论习题参考解答]习题7.3
查看>>
【BZOJ】1046: [HAOI2007]上升序列(dp)
查看>>
罗兰管弦乐音色表【中英文对照】 ----转载
查看>>
关于Boot应用中集成Spring Security你必须了解的那些事
查看>>
绑定到列表的指定元素
查看>>
Android RecyclerView使用GridLayoutManager导致间隙变大的问题
查看>>
如何使用jackson美化输出json/xml
查看>>
如何实现测试系统题目的自动推荐?
查看>>
oracle的case when的用法和decode函数的用法
查看>>
850 USB 烧录模式
查看>>
sam9260 闲鱼
查看>>