-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecorator.dart
82 lines (61 loc) · 1.6 KB
/
Decorator.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'package:design_pattern_dart/Display/Example.dart';
class Decorator extends Example {
Decorator([String filePath = "lib/Structural/Decorator.dart"])
: super(filePath);
@override
String testRun() {
// 第一杯我想要加牛奶就好
Coffee coffee1 = GeneralCoffee();
coffee1 = Milk(coffee1);
// 第二杯我想要全加
Coffee coffee2 = GeneralCoffee();
coffee2 = Milk(coffee2);
coffee2 = Whip(coffee2);
coffee2 = Vanilla(coffee2);
return """
${coffee1.getDescription()}
Price : ${coffee1.getCost()}
${coffee2.getDescription()}
Price: ${coffee2.getCost()}
""";
}
}
// 咖啡可以加料,加料要多加價,那應該怎麼做呢
abstract class Coffee {
double getCost();
String getDescription();
}
// 一般咖啡做為基底,讓他可以被加入各式調味料
class GeneralCoffee implements Coffee {
@override
double getCost() => 5;
@override
String getDescription() => "Normal Coffee";
}
// 牛奶
class Milk implements Coffee {
Coffee base;
Milk(this.base);
@override
double getCost() => base.getCost() + 2;
@override
String getDescription() => base.getDescription() + ", Milk";
}
// 鮮奶油
class Whip implements Coffee {
Coffee base;
Whip(this.base);
@override
double getCost() => base.getCost() + 3;
@override
String getDescription() => base.getDescription() + ", Whip";
}
// 香草
class Vanilla implements Coffee {
Coffee base;
Vanilla(this.base);
@override
double getCost() => base.getCost() + 4;
@override
String getDescription() => base.getDescription() + ", Vanilla";
}