-
Notifications
You must be signed in to change notification settings - Fork 50
/
proxy.php
64 lines (53 loc) · 1.47 KB
/
proxy.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
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/8/6
* Time: 01:34
* 代理模式
* 为其他对象提供一种代理以控制对这个对象的访问。
*/
class Printer {
//代理对象,一台打印机
public function printSth($args) {
echo $args[0].', I can print'.PHP_EOL;
}
}
class TextShop {
//这是一个文印处理店,只文印,卖纸,不照相
private $printer;
public function __construct(Printer $printer) {
$this->printer = $printer;
}
public function sellPaper() {
//卖纸
echo 'give you some paper ';
}
public function __call($method, $args) {
//将代理对象有的功能交给代理对象处理
if (method_exists($this->printer, $method)) {
$this->printer->$method($args);
}
}
}
class PhotoShop {
//这是一个照相店,只文印,拍照,不卖纸
private $printer;
public function __construct(Printer $printer) {
$this->printer = $printer;
}
public function takePhotos() { //照相
echo 'take photos for you ';
}
public function __call($method, $args) {
//将代理对象有的功能交给代理对象处理
if (method_exists($this->printer, $method)) {
$this->printer->$method($args);
}
}
}
$printer = new Printer();
$textShop = new TextShop($printer);
$photoShop = new PhotoShop($printer);
$textShop->printSth('textShop');
$photoShop->printSth('photoShop');