-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdecorator.php
60 lines (47 loc) · 1.13 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
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/7/25
* Time: 01:18
* 装饰器模式
* 装饰器模式能够从一个对象的外部动态地给对象添加功能。
*/
interface render{
public function rendData();
}
class webService implements render{
protected $data;
public function __construct($data) {
$this->data = $data;
}
public function rendData() {
return $this->data;
}
}
abstract class decorator implements render {
protected $wrapped;
public function __construct(render $wrappable) {
$this->wrapped = $wrappable;
}
}
class rendXml extends decorator{
public function rendData() {
$output = $this->wrapped->rendData();
foreach ($output as $val){
//
}
echo 'save xml';
}
}
class rendJson extends decorator{
public function rendData() {
$output = $this->wrapped->rendData();
echo json_encode($output);
}
}
$server = new webService(['name'=>'lock']);
$xmlServer = new rendXml($server);
echo $xmlServer->rendData();
$jsonServer = new rendJson($server);
echo $jsonServer->rendData();