gpt4 book ai didi

php设计模式 Decorator(装饰模式)

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章php设计模式 Decorator(装饰模式)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

代码如下

<?php  /**  * 装饰模式  *  * 动态的给一个对象添加一些额外的职责,就扩展功能而言比生成子类方式更为灵活  */  header("Content-type:text/html;charset=utf-8");  abstract class MessageBoardHandler  {  public function __construct(){}  abstract public function filter($msg);  }  class MessageBoard extends MessageBoardHandler  {  public function filter($msg)  {  return "处理留言板上的内容|".$msg;  }  }  $obj = new MessageBoard();  echo $obj->filter("一定要学好装饰模式<br/>");  // --- 以下是使用装饰模式 ----  class MessageBoardDecorator extends MessageBoardHandler  {  private $_handler = null;  public function __construct($handler)  {  parent::__construct();  $this->_handler = $handler;  }  public function filter($msg)  {  return $this->_handler->filter($msg);  }  }  // 过滤html  class HtmlFilter extends MessageBoardDecorator  {  public function __construct($handler)  {  parent::__construct($handler);  }  public function filter($msg)  {  return "过滤掉HTML标签|".parent::filter($msg);; // 过滤掉HTML标签的处理 这时只是加个文字 没有进行处理  }  }  // 过滤敏感词  class SensitiveFilter extends MessageBoardDecorator  {  public function __construct($handler)  {  parent::__construct($handler);  }  public function filter($msg)  {  return "过滤掉敏感词|".parent::filter($msg); // 过滤掉敏感词的处理 这时只是加个文字 没有进行处理  }  }  $obj = new HtmlFilter(new SensitiveFilter(new MessageBoard()));  echo $obj->filter("一定要学好装饰模式!<br/>");  。

最后此篇关于php设计模式 Decorator(装饰模式)的文章就讲到这里了,如果你想了解更多关于php设计模式 Decorator(装饰模式)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com