PHP设计模式-装饰器 发表于 2017-03-26 | 分类于 PHP | 评论数: | 阅读次数: 本文字数: 1.9k | 阅读时长 ≈ 2 分钟 适用性 对现有的对象增加功能 和适配器的区别:适配器是连接两个接口,装饰器是对现有的对象包装 代码示例123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121/** * 鞋接口 */interface ShoesInterface{ public function product();}/** * 装饰器抽象类. */abstract class Decorator implements ShoesInterface{ /** * 产品生产线对象 */ protected $shoes; /** * 构造函数. */ public function __construct(ShoesInterface $shoes) { $this->shoes = $shoes; } /** * 生产. */ public function product() { $this->shoes->product(); } /** * 装饰操作. */ abstract public function decorate($value);}/** * 贴标装饰器 */class DecoratorBrand extends Decorator{ private $_brand; /** * 构造函数 */ public function __construct(ShoesInterface $phone) { $this->phone = $phone; } public function __set($name='', $value='') { $this->$name = $value; } /** * 生产 */ public function product() { $this->phone->product(); $this->decorate($this->_brand); } /** * 贴标操作 */ public function decorate($value='') { echo "贴上{$value}标志 \n"; }}/** * 滑板鞋实体 */class ShoesSkateboard implements ShoesInterface{ public function product() { echo "生产一滑板鞋"; }}/** * 运动鞋实体 */class ShoesSport implements ShoesInterface{ public function product() { echo "生产一双球鞋"; }}try { echo "未加装饰器之前:\n"; // 生产运动鞋 $shoesSport = new ShoesSport(); $shoesSport->product(); echo "\n--------------------\n"; echo "加贴标装饰器:\n"; // 初始化一个贴商标适配器 $DecoratorBrand = new DecoratorBrand(new ShoesSport()); $DecoratorBrand->_brand = 'nike'; // 生产nike牌运动鞋 $DecoratorBrand->product();} catch (\Exception $e) { echo $e->getMessage();}