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

