-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuilder.dart
78 lines (63 loc) · 1.45 KB
/
Builder.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
import 'package:design_pattern_dart/Display/Example.dart';
class Builder extends Example {
Builder([String filePath = "lib/Creational/Builder.dart"]) : super(filePath);
@override
String testRun() {
var noLettuceBurger =
BurgerBuilder(15)
.addBeef()
.addCheese()
.addTomato()
.build();
return noLettuceBurger.content();
}
}
class Burger {
double _size;
bool _cheese;
bool _beef;
bool _lettuce;
bool _tomato;
Burger._();
// Not good: Burger(this._size, this._cheese, this._beef, this._lettuce, this._tomato);
Burger(BurgerBuilder builder) {
_size = builder._size;
_cheese = builder._cheese;
_beef = builder._beef;
_lettuce = builder._lettuce;
_tomato = builder._tomato;
}
String content() => """
Your Burger size: $_size,
Cheese: $_cheese,
Beef: $_beef,
Lettuce: $_lettuce,
Tomato: $_tomato.
""";
}
// 利用 Builder 方式來建構物件,最後再以 build() 返回
class BurgerBuilder {
double _size;
bool _cheese = false;
bool _beef = false;
bool _lettuce = false;
bool _tomato = false;
BurgerBuilder(this._size);
BurgerBuilder addCheese() {
_cheese = true;
return this;
}
BurgerBuilder addBeef() {
_beef = true;
return this;
}
BurgerBuilder addLettuce() {
_lettuce = true;
return this;
}
BurgerBuilder addTomato() {
_tomato = true;
return this;
}
Burger build() => Burger(this);
}