-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainOfResponsibility.php
More file actions
86 lines (76 loc) · 2.4 KB
/
ChainOfResponsibility.php
File metadata and controls
86 lines (76 loc) · 2.4 KB
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
<?php
/*
*责任链模式
*
*现实生活示例
*例如,你的帐户中有三种付款方式(A,B 和 C); 每种方式付款额不同。
*A 可支付 100 美元,B 可支付 300 美元,C 可支付 1000 美元,支付的优先级为 A->B->C。
*现在想要购买价值 210 美元的东西。使用责任链模式,首先将检查帐户 A 是否可以进行购买,
*如果可以购买,链条将被破坏。如果不能购买,将继续检查账号 B 是否可以购买,
*如果可以购买,链条将被破坏,否则请求将继续转发,直到找到合适的处理程序。
*这里的 A、B 和 C 就是责任链的链条,整个现象就是责任链模式。
*
*概述
*责任链模式有助于建立一个对象链。请求从一端进入,在对象之间转发,直到找到合适的处理程序。
*维基百科
*责任链模式是面向对象程序设计的一种软件设计模式,它包含了一些命令对象和一系列的处理对象。
*每一个处理对象决定它能处理哪些命令对象,不能处理的命令对象传递给该链中的下一个处理对象。
*
*/
//程序示例
//以上面的支付账号为例,首先给出账户基类,包含链接账号的逻辑以及一些不同类型的账户
abstract class Account
{
protected $successor;
protected $balance;
public function setNext(Account $account)
{
$this->successor = $account;
}
public function pay(float $amountToPay)
{
if ($this->canPay($amountToPay)) {
echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay, get_called_class());
}elseif ($this->successor) {
echo sprintf('Cannot pay using %s Proceeding...' . PHP_EOL, get_called_class());
$this->successor->pay($amountToPay);
}else{
throw new Exception('None of the accounts have enough balance');
}
}
public function canPay($amount):bool
{
return $this->balance >= $amount;
}
}
class Bank extends Account
{
protected $balance;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
class Paypal extends Account
{
protected $balance;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
class Bitcoin extends Account
{
protected $balance;
public function __construct(float $balance)
{
$this->balance = $balance;
}
}
//使用
$bank = new Bank(100);
$paypal = new Paypal(200);
$bitcoin = new Bitcoin(300);
$bank->setNext($paypal);
$paypal->setNext($bitcoin);
$bank->pay(259);