-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProxy.dart
56 lines (43 loc) · 1.14 KB
/
Proxy.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
import 'package:design_pattern_dart/Display/Example.dart';
class Proxy extends Example {
Proxy([String filePath = "lib/Structural/Proxy.dart"]) : super(filePath);
@override
String testRun() {
Door labDoor = LabDoor();
SecuredDoor securedDoor = SecuredDoor(labDoor);
return """
// Thief is trying to break the door but failed.
${securedDoor.open("123456")}
// Teacher can open the door easily.
${securedDoor.open("abcdefg")}
${securedDoor.close()}
""";
}
}
abstract class Door {
String open();
String close();
}
// 這是一個沒有防盜的實驗室大門
class LabDoor implements Door {
@override
String close() => "Lab door is closing!";
@override
String open() => "Lab door is opening!";
}
// 我們利用代理模式,使防盜門代理實驗室大門的功能。
class SecuredDoor {
Door door;
SecuredDoor(this.door);
String open(String password) {
if (authenticate(password)) {
return door.open();
} else {
return "YOU SHALL NOT PASS!";
}
}
String close() => door.close();
bool authenticate(String password) {
return password == "abcdefg";
}
}