PHP简单装饰器模式实现与用法示例
程序员文章站
2024-03-12 11:56:14
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
上一篇: java实现文件断点续传下载功能