-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChainOfResponsibility.dart
60 lines (48 loc) · 1.43 KB
/
ChainOfResponsibility.dart
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
import 'package:design_pattern_dart/Display/Example.dart';
class ChainOfResponsibility extends Example {
ChainOfResponsibility(
[String filePath = "lib/Behavioral/ChainOfResponsibility.dart"])
: super(filePath);
@override
String testRun() {
Account bank = Bank(100);
Account paypal = Paypal(200);
Account bitcoin = Bitcoin(300);
bank.setNext(paypal);
paypal.setNext(bitcoin);
return bank.pay(259);
}
}
// 建立三個帳戶到 chain 中
abstract class Account {
Account _successor;
double _balance;
Account(this._balance);
// 讓客戶端能自由調整 chain 的下一個物件
void setNext(Account next) {
_successor = next;
}
// 現在的帳戶不夠錢就交給下一個帳戶
String pay(double amountToPay) {
if (canPay(amountToPay)) {
return "Paid $amountToPay using ${this.runtimeType.toString()}";
} else if (_successor != null) {
var reject =
"Cannot pay using ${this.runtimeType.toString()}. Proceeding ... \n";
reject += _successor.pay(amountToPay);
return reject;
} else {
return "None of the accounts have enough balance";
}
}
bool canPay(double amountToPay) => _balance >= amountToPay;
}
class Bank extends Account {
Bank(double balance) : super(balance);
}
class Paypal extends Account {
Paypal(double balance) : super(balance);
}
class Bitcoin extends Account {
Bitcoin(double balance) : super(balance);
}