1 <?php
2 //装饰器模式-在不改变原有类的结构上,对类的功能那个作补充
3
4 //武器基类
5 abstract class Weapon{
6 abstract public function descriptions();
7 abstract public function cost();
8 }
9
10 //剑类
11 class Glave extends Weapon{
12 public function descriptions(){
13 return 'Glave';
14 }
15
16 public function cost(){
17 return "100";
18 }
19 }
20
21 //匕首类
22 class Knife extends Weapon{
23 public function descriptions(){
24 return __CLASS__;
25 }
26 public function cost(){
27 return "80";
28 }
29 }
30
31 //斧类
32 class Axe extends Weapon{
33 public function descriptions(){
34 return __CLASS__;
35 }
36 public function cost(){
37 return "200";
38 }
39 }
40
41 //属性类
42 class Property extends Weapon{
43 protected $_weapon = null;
44 protected $_price = 0;
45 protected $_descriptions = '';
46 public function __construct(Weapon $weapon){
47 $this->_weapon = $weapon;
48 }
49 public function cost(){
50 return $this->_weapon->cost() + $this->_price;
51 }
52
53 public function descriptions(){
54 return $this->_weapon->descriptions().$this->_descriptions;
55 }
56 }
57
58 //力量属性
59 class Strength extends Property{
60 protected $_price = 30;
61 protected $_descriptions = '+ Strength';
62 }
63
64 //敏捷属性
65 class Agility extends Property{
66 protected $_price = 50;
67 protected $_descriptions = '+ Agility';
68 }
69
70 //智力属性
71 class Intellect extends Property{
72 protected $_price = 20;
73 protected $_descriptions = '+ Intellect';
74 }
75
76 $weapon = new Agility(new Strength(new Strength(new Glave())));
77 echo $weapon->cost();
78 echo $weapon->descriptions();