<?php
/**
* 2020-6-3 Xun
* decorate装饰器模式(通过文章修饰来举例)
* 装饰器模式就是为了让类不用一直继承来实现对数据的添加而且这样也无法做到不同顺序的装饰
* 通过装饰器模式我们可以实现需要怎样装饰我们的数据就使用什么样的类来装饰
*/
//构建文章类
class Article{
//定义文章内容属性
protected $text;
//定义存储文章类属性
protected $art=null;
//构造函数为文章属性赋值
public function __construct($text)
{
//给文章属性赋值
$this->text=$text;
}
//返回文章内容
public function show()
{
return $this->text;
}
}
//npc装饰类
class Npc extends Article {
public function __construct($art)
{
//给文章类属性赋值
$this->art=$art;
//返回装饰后的内容
$this->text=$this->art->show().'Npc装饰过了';
}
}
//Blsd装饰类
class Blsd extends Article {
public function __construct($art)
{
//给文章类属性赋值
$this->art=$art;
//返回装饰后的内容
$this->text=$this->art->show().'Blsd装饰过了';
}
}
//实例化文章类
//$obj=new Article('我永远喜欢菠萝赛东');
//echo $obj->show();
//使用装饰器模式实现装饰
$obj=new Blsd(new Npc(new Article('菠萝赛东')));
echo $obj->show();