您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
设计模式-装饰模式完成文章编辑
发布时间:2017-11-30 09:28:33编辑:雪饮阅读()
<?php
header('content-type:text/html;charset=utf-8');
//装饰器模式做文章修饰功能
class BaseArt{
protected $content;
protected $art=null;
public function __construct($content){
$this->content=$content;
}
public function decorator(){
return $this->content;
}
}
//编辑文章摘要
class BianArt extends BaseArt{
public function __construct(BaseArt $art){
$this->art=$art;
$this->decorator();
}
public function decorator(){
return $this->content=$this->art->decorator().'---小编摘要';
}
}
//seo关键词
class SEOArt extends BaseArt{
public function __construct(BaseArt $art){
$this->art=$art;
$this->decorator();
}
public function decorator(){
return $this->content=$this->art->decorator().'---SEO关键词';
}
}
//调用
//编辑一片文章
$art=new BaseArt('天天向上');
echo $art->decorator()."<br/>";
//添加摘要
$art=new BianArt($art);
echo $art->decorator()."<br/>";
//添加seo关键词
$art=new SEOArt($art);
echo $art->decorator();
?>
关键字词:设计模式,装饰模式
上一篇:设计模式-责任链模式完成举报功能
下一篇:设计模式