首页 > Web开发 > 详细

php设计模式之装饰模式实例代码

时间:2020-01-12 17:43:33      阅读:82      评论:0      收藏:0      [点我收藏+]
<?php 
header("Content-type:text/html;charset=utf-8");

/**
* 文章编辑类
*/
class Article
{
    protected $content;
    protected $art = null;
    public function __construct($content)
    {
        $this->content = $content;
    }
    public function decorator(){
        return $this->content;
    }
}


/**
* 小编加个摘要
*/
class BianArticle extends Article
{
    public function __construct(Article $art){
        $this->art = $art;
        $this->decorator();
    }

    public function decorator()
    {
        return $this->content = $this->art->content."小编加了个摘要<br>";
    }
}


/**
* SEO对文章描述做了个修改
*/
class SEOArticle extends Article
{
    public function __construct(Article $art){
        $this->art = $art;
        $this->decorator();
    }

    public function decorator()
    {
        return $this->content = $this->art->content."SEO对文章描述做了个修改<br>";
    }
}

$b = new SEOArticle(new BianArticle(new Article("完成了文章编辑<br>")));
echo $b->decorator();

php设计模式之装饰模式实例代码

原文:https://www.cnblogs.com/Mishell/p/12182892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!