php 装饰_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程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值