-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdelegation.php
78 lines (71 loc) · 1.48 KB
/
delegation.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
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/8/14
* Time: 16:45
* 委托模式
* 委托是对一个类的功能进行扩展和复用的方法。
* 它的做法是:写一个附加的类提供附加的功能,并使用原来的类的实例提供原有的功能。
*/
class Bank {
protected $info;
/**
* @param $type
* @param $money
*/
public function updateBrankInfo($type, $money) {
$this->info[$type] = $money;
}
/**
* 相关操作(包括存款、取款操作)
* @param $branktype
* @return mixed
*/
public function brankWithdraw($branktype) {
$obj = new $branktype;
return $obj->brankMain($this->info);
}
}
/**
* 委托接口
* Interface Delegate
*/
interface Delegate {
public function brankMain($info);
}
/**
* 存款操作类
* Class brankDeposit
*/
class brankDeposit implements Delegate {
/**
* 存款操作
* @param $info
*/
public function brankMain($info) {
echo $info['deposit'];
}
}
/**
* 取款操作类
* Class brankWithdraw
*/
class brankWithdraw implements Delegate {
/**
* 取款操作
* @param $info
*/
public function brankMain($info) {
echo $info['withdraw'];
}
}
/*
客户端测试代码:
*/
$bank = new Bank();
$bank->updateBrankInfo("deposit", "4000");
$bank->updateBrankInfo("withdraw", "2000");
$bank->brankWithdraw("brankDeposit");
echo PHP_EOL;
$bank->brankWithdraw("brankWithdraw");