设置模式之装饰模式
什么事装饰模式呢?其实装饰模式就是为一个对象穿上不同的衣服。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
abstract class food{
2
3
public $des="食物";
4
public $cost;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
function getDes()
7
{
8
return $this->des;
9
}
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
function getCost()
12
{
13
return $this->cost;
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
class rice extends food {
20
21
function __construct()
22
{
23
$this->des ="米饭";
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
$this->cost="0.44";
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
}
31![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
class sub extends food{
33![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
public $obj;
35![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
function __construct()
37
{
38![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
}
40
}
41![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
class potato extends sub {
43![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
function __construct($obj)
47
{
48
$this->obj =$obj;
49
$this->des ="土豆丝";
50
$this->cost =0.10;
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
function getDes()
54
{
55
return $this->obj->getDes().$this->des;
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
function getCost()
59
{
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
62
return $this->obj->getCost()+$this->cost;
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
class xiancai extends sub {
68![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71
function __construct($obj)
72
{
73
$this->obj =$obj;
74
$this->des ="咸菜";
75
$this->cost =0.10;
76
}
77![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
78
function getDes()
79
{
80
return $this->obj->getDes().$this->des;
81
}
82![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83
function getCost()
84
{
85
return $this->obj->getCost()+$this->cost;
86
}
87![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
88
}
89![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
90![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
$eat = new rice();
92
$eat = new tomoto($eat);
93
94
$eat = new xiancai($eat);
95![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
96
// print_r($eat);
97![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
98
echo $eat->getDes();
99![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
100
echo $eat->getCost();显示的结果为 米饭土豆丝咸菜
0.64
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
0.64