PHP簡單裝飾器模式實現(xiàn)與用法示例
本文實例講述了PHP簡單裝飾器模式實現(xiàn)與用法。分享給大家供大家參考,具體如下:
<?php //裝飾器模式-在不改變原有類的結(jié)構(gòu)上,對類的功能那個作補充 //武器基類 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();
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php jq jquery getJSON跨域提交數(shù)據(jù)完整版
getJSON跨域提交數(shù)據(jù),想必大家已在很多文章中見到過,下面的示例是php jq jquery getJSON跨域提交數(shù)據(jù)完整代碼,感興趣的朋友可以參考下2013-09-09php多進程模擬并發(fā)事務(wù)產(chǎn)生的問題小結(jié)
這篇文章主要給大家介紹了關(guān)于php多進程模擬并發(fā)事務(wù)產(chǎn)生的問題,文中通過示例代碼介紹的非常想吃詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12PHP經(jīng)典設(shè)計模式之依賴注入定義與用法詳解
這篇文章主要介紹了PHP經(jīng)典設(shè)計模式之依賴注入,結(jié)合實例形式分析了php依賴注入的定義、原理與用法,需要的朋友可以參考下2019-05-05SESSION信息保存在哪個文件目錄下以及能夠用來保存什么類型的數(shù)據(jù)
session默認是保存到c:\windows\temp目錄下,但是通過修改php.ini中的session.save_path值可以改變session的保存路徑2012-06-06