-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decorator.php
91 lines (75 loc) · 1.28 KB
/
Decorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* 接口
*/
interface Shape
{
public function draw();
}
/**
* 具体实现类 圆
*/
class Circle implements Shape
{
function __construct()
{
# code...
}
public function draw()
{
echo "draw a circle ! \n";
}
}
/**
* 具体实现类 方框
*/
class Rectangle implements Shape
{
function __construct()
{
# code...
}
public function draw ()
{
echo "draw a Rectangle! \n";
}
}
/**
* 抽象装饰器类
*/
abstract class ShapeDecorator implements Shape
{
protected $shape;
function __construct(Shape $shape)
{
$this->shape = $shape;
}
public function draw()
{
$this->shape->draw();
}
}
/**
* 装饰器的实现类
*/
class RedShapeDecorator extends ShapeDecorator
{
public function draw()
{
$this->shape->draw();
$this->setColor();
}
protected function setColor()
{
echo "set the color red ! \n";
}
}
$circle = new Circle();
$rectangle = new Rectangle();
$circle->draw();
$rectangle->draw();
$circleDec = new RedShapeDecorator(new Circle());
$rectangleDec = new RedShapeDecorator(new Rectangle());
$circleDec->draw();
$rectangleDec->draw();
?>