春天,大自然一片生机,天空则设计了一个优美的背景。淡蓝的颜色,有些透明,像一块在强烈光照下的蓝宝石,无暇的找不出半点疵瑕。云儿也不知上哪儿玩去了,剩下个特大特明亮的太陽,照得天空泛白。万物在这背景下都是那么的奇妙美好,无处不弥漫着陽光的气味。天空,多么像一位诗人,创造了春天这么美的意境!
本文实例讲述了PHP简单装饰器模式实现与用法。分享给大家供大家参考,具体如下:
<?php
//装饰器模式-在不改变原有类的结构上,对类的功能那个作补充
//武器基类
abstract class Weapon{
abstract public function descriptions();
abstract public function cost();
}
//剑类
class Glave extends Weapon{
public function descriptions(){
return 'Glave';
}
public function cost(){
return "100";
}
}
//匕首类
class Knife extends Weapon{
public function descriptions(){
return __CLASS__;
}
public function cost(){
return "80";
}
}
//斧类
class Axe extends Weapon{
public function descriptions(){
return __CLASS__;
}
public function cost(){
return "200";
}
}
//属性类
class Property extends Weapon{
protected $_weapon = null;
protected $_price = 0;
protected $_descriptions = '';
public function __construct(Weapon $weapon){
$this->_weapon = $weapon;
}
public function cost(){
return $this->_weapon->cost() + $this->_price;
}
public function descriptions(){
return $this->_weapon->descriptions().$this->_descriptions;
}
}
//力量属性
class Strength extends Property{
protected $_price = 30;
protected $_descriptions = '+ Strength';
}
//敏捷属性
class Agility extends Property{
protected $_price = 50;
protected $_descriptions = '+ Agility';
}
//智力属性
class Intellect extends Property{
protected $_price = 20;
protected $_descriptions = '+ Intellect';
}
$weapon = new Agility(new Strength(new Strength(new Glave())));
echo $weapon->cost();
echo $weapon->descriptions();
希望本文所述对大家PHP程序设计有所帮助。
本文PHP简单装饰器模式实现与用法示例到此结束。当你尽了自我的最大努力时,失败也是伟大的。小编再次感谢大家对我们的支持!