概念
装饰器模式(decorator pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
uml图
角色
抽象组件角色(component):定义一个对象接口,以规范准备接受附加责任的对象,即可以给这些对象动态地添加职责。
具体组件角色(concretecomponent) :被装饰者,定义一个将要被装饰增加功能的类。可以给这个类的对象添加一些职责
抽象装饰器(decorator):维持一个指向构件component对象的实例,并定义一个与抽象组件角色component接口一致的接口
具体装饰器角色(concretedecorator):向组件添加职责。
适用场景
需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
代码
<?php
header('content-type:text/html;charset=utf-8');
/**
* 装饰器模式
*/
/**
* interface icomponent 组件对象接口
*/
interface icomponent
{
public function display();
}
/**
* class person 待装饰对象
*/
class person implements icomponent
{
private $_name;
/**
* person constructor. 构造方法
*
* @param $name 对象人物名称
*/
public function __construct($name)
{
$this->_name = $name;
}
/**
* 实现接口方法
*/
public function display()
{
echo "装扮者:{$this->_name}<br/>";
}
}
/**
* class clothes 所有装饰器父类-服装类
*/
class clothes implements icomponent
{
protected $component;
/**
* 接收装饰对象
*
* @param icomponent $component
*/
public function decorate(icomponent $component)
{
$this->component = $component;
}
/**
* 输出
*/
public function display()
{
if(!empty($this->component))
{
$this->component->display();
}
}
}
/**
* 下面为具体装饰器类
*/
/**
* class sneaker 运动鞋
*/
class sneaker extends clothes
{
public function display()
{
echo "运动鞋 ";
parent::display();
}
}
/**
* class tshirt t恤
*/
class tshirt extends clothes
{
public function display()
{
echo "t恤 ";
parent::display();
}
}
/**
* class coat 外套
*/
class coat extends clothes
{
public function display()
{
echo "外套 ";
parent::display();
}
}
/**
* class trousers 裤子
*/
class trousers extends clothes
{
public function display()
{
echo "裤子 ";
parent::display();
}
}
/**
* 客户端测试代码
*/
class client
{
public static function test()
{
$zhangsan = new person('张三');
$lisi = new person('李四');
$sneaker = new sneaker();
$coat = new coat();
$sneaker->decorate($zhangsan);
$coat->decorate($sneaker);
$coat->display();
echo "<hr/>";
$trousers = new trousers();
$tshirt = new tshirt();
$trousers->decorate($lisi);
$tshirt->decorate($trousers);
$tshirt->display();
}
}
client::test();
运行结果:
外套 运动鞋 装扮者:张三
t恤 裤子 装扮者:李四