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)文章
centos下file_put_contents()無法寫入文件的原因及解決方法
下面小編就為大家?guī)硪黄猚entos下file_put_contents()無法寫入文件的原因及解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
php數(shù)組函數(shù)序列之a(chǎn)rray_combine() - 數(shù)組合并函數(shù)使用說明
array_combine() 函數(shù)通過合并兩個數(shù)組來創(chuàng)建一個新數(shù)組,其中的一個數(shù)組是鍵名,另一個數(shù)組的值為鍵值2011-10-10
PHP靜態(tài)方法和靜態(tài)屬性及常量屬性的區(qū)別與介紹
今天小編就為大家分享一篇關(guān)于PHP靜態(tài)方法和靜態(tài)屬性及常量屬性的區(qū)別與介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
php中將字符串轉(zhuǎn)為HTML的實體引用的一個類
php將字符串轉(zhuǎn)為HTML的實體引用的一個類,有需要的朋友可以參考下2013-02-02

