????????裝飾模式是以對(duì)客戶透明的方式動(dòng)態(tài)地給一個(gè)對(duì)象附加上更多的職責(zé)睁搭。這也就是說(shuō),客戶端并不會(huì)覺(jué)得對(duì)象在裝飾前和裝飾后有什么不同耍群。裝飾模式可以在不使用創(chuàng)造更多子類(lèi)的情況下香椎,將對(duì)象的功能加以擴(kuò)展。
裝飾模式中主要角色
????????抽象構(gòu)件(Component)角色:定義一個(gè)對(duì)象接口囚企,以規(guī)范準(zhǔn)備接收附加職責(zé)的對(duì)象,從而可以給這些對(duì)象動(dòng)態(tài)地添加職責(zé)瑞眼。
????????具體構(gòu)件(Concrete Component)角色:定義一個(gè)將要接收附加職責(zé)的類(lèi)龙宏。
????????裝飾(Decorator)角色:持有一個(gè)指向Component對(duì)象的指針,并定義一個(gè)與Component接口一致的接口伤疙。
????????具體裝飾(Concrete Decorator)角色:負(fù)責(zé)給構(gòu)件對(duì)象增加附加的職責(zé)烦衣。
裝飾模式的優(yōu)缺點(diǎn)
裝飾模式的優(yōu)點(diǎn):
1掩浙、比靜態(tài)繼承更靈活花吟;
2、避免在層次結(jié)構(gòu)高層的類(lèi)有太多的特征
裝飾模式的缺點(diǎn):
使用裝飾模式會(huì)產(chǎn)生比使用繼承關(guān)系更多的對(duì)象厨姚。并且這些對(duì)象看上去都很想像衅澈,從而使得查錯(cuò)變得困難。
裝飾模式適用場(chǎng)景
1今布、在不影響其他對(duì)象的情況下经备,以動(dòng)態(tài)、透明的方式給單個(gè)對(duì)象添加職責(zé)部默。
2侵蒙、處理那些可以撤消的職責(zé),即需要?jiǎng)討B(tài)的給一個(gè)對(duì)象添加功能并且這些功能是可以動(dòng)態(tài)的撤消的傅蹂。
3纷闺、當(dāng)不能彩生成子類(lèi)的方法進(jìn)行擴(kuò)充時(shí)。一種情況是份蝴,可能有大量獨(dú)立的擴(kuò)展犁功,為支持每一種組合將產(chǎn)生大量的子類(lèi),使得子類(lèi)數(shù)目呈爆炸性增長(zhǎng)婚夫。另一種情況可能是因?yàn)轭?lèi)定義被隱藏浸卦,或類(lèi)定義不能用于生成子類(lèi)。
裝飾模式PHP示例
/**?* 抽象構(gòu)件角色?*/
interface Component
{?
????????/**??* 示例方法??*/?
????????public function operation();
}
/**?* 裝飾角色?*/
abstract class Decorator implements Component
{?
????????protected $_component;?
????????public function __construct(Component $component)
????????{??
????????????????$this->_component = $component;?
????????}?
????????public function operation()
????????{??
????????????????$this->_component->operation();?
????????}
}
/**?* 具體裝飾類(lèi)A?*/
class ConcreteDecoratorA extends Decorator
{?
????????public function __construct(Component $component)
????????{??
????????????????parent::__construct($component);?
????????}?
????????public function operation()
????????{??
????????????????parent::operation();
????????????????// 調(diào)用裝飾類(lèi)的操作??
????????????????$this->addedOperationA();
????????????????// 新增加的操作?
????????}?
????????/**??* 新增加的操作A限嫌,即裝飾上的功能??*/?
????????public function addedOperationA()
????????{
????????????????echo 'Add Operation A?';?
????????}
}
/**?* 具體裝飾類(lèi)B?*/
class ConcreteDecoratorB extends Decorator
{?
????????public function __construct(Component $component)
????????{??
????????????????parent::__construct($component);?
????????}?
????????public function operation()
????????{??
????????????????parent::operation();??
????????????????$this->addedOperationB();?
????????}?
????????/**??* 新增加的操作B,即裝飾上的功能??*/?
????????public function addedOperationB()
????????{??
????????????????echo 'Add Operation B?';?
????????}
}
/**?* 具體構(gòu)件?*/
class ConcreteComponent implements Component
{?
????????public function operation()
????????{??
????????????????echo 'Concrete Component operation?';?
????????}
}
/**?* 客戶端?*/
class Client
{??
????????/**??* Main program.??*/?
????????public static function main()
????????{??
????????????????$component = new ConcreteComponent();??
????????????????$decoratorA = new ConcreteDecoratorA($component);??
????????????????$decoratorB = new ConcreteDecoratorB($decoratorA);??
????????????????$decoratorA->operation();??
????????????????$decoratorB->operation();?
????????}
}
Client::main();
裝飾器模式-在不改變?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();
????????圖片與文章無(wú)關(guān)匣椰,在閱讀技術(shù)文檔的時(shí)候欣賞一下美女也是一個(gè)不錯(cuò)的選擇裆熙,希望可以對(duì)你有幫助,如果有錯(cuò)誤的地方歡迎指正禽笑。